stk-webcam.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
  3. *
  4. * Copyright (C) 2006 Nicolas VIVIEN
  5. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  6. *
  7. * Some parts are inspired from cafe_ccic.c
  8. * Copyright 2006-2007 Jonathan Corbet
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/dmi.h>
  30. #include <linux/usb.h>
  31. #include <linux/mm.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/videodev2.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-event.h>
  37. #include "stk-webcam.h"
  38. static int hflip = -1;
  39. module_param(hflip, int, 0444);
  40. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 0");
  41. static int vflip = -1;
  42. module_param(vflip, int, 0444);
  43. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 0");
  44. static int debug;
  45. module_param(debug, int, 0444);
  46. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  47. MODULE_LICENSE("GPL");
  48. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  49. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  50. /* Some cameras have audio interfaces, we aren't interested in those */
  51. static struct usb_device_id stkwebcam_table[] = {
  52. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  53. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  54. { }
  55. };
  56. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  57. /*
  58. * The stk webcam laptop module is mounted upside down in some laptops :(
  59. *
  60. * Some background information (thanks to Hans de Goede for providing this):
  61. *
  62. * 1) Once upon a time the stkwebcam driver was written
  63. *
  64. * 2) The webcam in question was used mostly in Asus laptop models, including
  65. * the laptop of the original author of the driver, and in these models, in
  66. * typical Asus fashion (see the long long list for uvc cams inside v4l-utils),
  67. * they mounted the webcam-module the wrong way up. So the hflip and vflip
  68. * module options were given a default value of 1 (the correct value for
  69. * upside down mounted models)
  70. *
  71. * 3) Years later I got a bug report from a user with a laptop with stkwebcam,
  72. * where the module was actually mounted the right way up, and thus showed
  73. * upside down under Linux. So now I was facing the choice of 2 options:
  74. *
  75. * a) Add a not-upside-down list to stkwebcam, which overrules the default.
  76. *
  77. * b) Do it like all the other drivers do, and make the default right for
  78. * cams mounted the proper way and add an upside-down model list, with
  79. * models where we need to flip-by-default.
  80. *
  81. * Despite knowing that going b) would cause a period of pain where we were
  82. * building the table I opted to go for option b), since a) is just too ugly,
  83. * and worse different from how every other driver does it leading to
  84. * confusion in the long run. This change was made in kernel 3.6.
  85. *
  86. * So for any user report about upside-down images since kernel 3.6 ask them
  87. * to provide the output of 'sudo dmidecode' so the laptop can be added in
  88. * the table below.
  89. */
  90. static const struct dmi_system_id stk_upside_down_dmi_table[] = {
  91. {
  92. .ident = "ASUS G1",
  93. .matches = {
  94. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  95. DMI_MATCH(DMI_PRODUCT_NAME, "G1")
  96. }
  97. }, {
  98. .ident = "ASUS F3JC",
  99. .matches = {
  100. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  101. DMI_MATCH(DMI_PRODUCT_NAME, "F3JC")
  102. }
  103. },
  104. {
  105. .ident = "T12Rg-H",
  106. .matches = {
  107. DMI_MATCH(DMI_SYS_VENDOR, "HCL Infosystems Limited"),
  108. DMI_MATCH(DMI_PRODUCT_NAME, "T12Rg-H")
  109. }
  110. },
  111. {}
  112. };
  113. /*
  114. * Basic stuff
  115. */
  116. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  117. {
  118. struct usb_device *udev = dev->udev;
  119. int ret;
  120. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  121. 0x01,
  122. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  123. value,
  124. index,
  125. NULL,
  126. 0,
  127. 500);
  128. if (ret < 0)
  129. return ret;
  130. else
  131. return 0;
  132. }
  133. int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
  134. {
  135. struct usb_device *udev = dev->udev;
  136. int ret;
  137. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  138. 0x00,
  139. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  140. 0x00,
  141. index,
  142. (u8 *) value,
  143. sizeof(u8),
  144. 500);
  145. if (ret < 0)
  146. return ret;
  147. else
  148. return 0;
  149. }
  150. static int stk_start_stream(struct stk_camera *dev)
  151. {
  152. int value;
  153. int i, ret;
  154. int value_116, value_117;
  155. if (!is_present(dev))
  156. return -ENODEV;
  157. if (!is_memallocd(dev) || !is_initialised(dev)) {
  158. STK_ERROR("FIXME: Buffers are not allocated\n");
  159. return -EFAULT;
  160. }
  161. ret = usb_set_interface(dev->udev, 0, 5);
  162. if (ret < 0)
  163. STK_ERROR("usb_set_interface failed !\n");
  164. if (stk_sensor_wakeup(dev))
  165. STK_ERROR("error awaking the sensor\n");
  166. stk_camera_read_reg(dev, 0x0116, &value_116);
  167. stk_camera_read_reg(dev, 0x0117, &value_117);
  168. stk_camera_write_reg(dev, 0x0116, 0x0000);
  169. stk_camera_write_reg(dev, 0x0117, 0x0000);
  170. stk_camera_read_reg(dev, 0x0100, &value);
  171. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  172. stk_camera_write_reg(dev, 0x0116, value_116);
  173. stk_camera_write_reg(dev, 0x0117, value_117);
  174. for (i = 0; i < MAX_ISO_BUFS; i++) {
  175. if (dev->isobufs[i].urb) {
  176. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  177. atomic_inc(&dev->urbs_used);
  178. if (ret)
  179. return ret;
  180. }
  181. }
  182. set_streaming(dev);
  183. return 0;
  184. }
  185. static int stk_stop_stream(struct stk_camera *dev)
  186. {
  187. int value;
  188. int i;
  189. if (is_present(dev)) {
  190. stk_camera_read_reg(dev, 0x0100, &value);
  191. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  192. if (dev->isobufs != NULL) {
  193. for (i = 0; i < MAX_ISO_BUFS; i++) {
  194. if (dev->isobufs[i].urb)
  195. usb_kill_urb(dev->isobufs[i].urb);
  196. }
  197. }
  198. unset_streaming(dev);
  199. if (usb_set_interface(dev->udev, 0, 0))
  200. STK_ERROR("usb_set_interface failed !\n");
  201. if (stk_sensor_sleep(dev))
  202. STK_ERROR("error suspending the sensor\n");
  203. }
  204. return 0;
  205. }
  206. /*
  207. * This seems to be the shortest init sequence we
  208. * must do in order to find the sensor
  209. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  210. * is also reset. Maybe powers down it?
  211. * Rest of values don't make a difference
  212. */
  213. static struct regval stk1125_initvals[] = {
  214. /*TODO: What means this sequence? */
  215. {0x0000, 0x24},
  216. {0x0100, 0x21},
  217. {0x0002, 0x68},
  218. {0x0003, 0x80},
  219. {0x0005, 0x00},
  220. {0x0007, 0x03},
  221. {0x000d, 0x00},
  222. {0x000f, 0x02},
  223. {0x0300, 0x12},
  224. {0x0350, 0x41},
  225. {0x0351, 0x00},
  226. {0x0352, 0x00},
  227. {0x0353, 0x00},
  228. {0x0018, 0x10},
  229. {0x0019, 0x00},
  230. {0x001b, 0x0e},
  231. {0x001c, 0x46},
  232. {0x0300, 0x80},
  233. {0x001a, 0x04},
  234. {0x0110, 0x00},
  235. {0x0111, 0x00},
  236. {0x0112, 0x00},
  237. {0x0113, 0x00},
  238. {0xffff, 0xff},
  239. };
  240. static int stk_initialise(struct stk_camera *dev)
  241. {
  242. struct regval *rv;
  243. int ret;
  244. if (!is_present(dev))
  245. return -ENODEV;
  246. if (is_initialised(dev))
  247. return 0;
  248. rv = stk1125_initvals;
  249. while (rv->reg != 0xffff) {
  250. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  251. if (ret)
  252. return ret;
  253. rv++;
  254. }
  255. if (stk_sensor_init(dev) == 0) {
  256. set_initialised(dev);
  257. return 0;
  258. } else
  259. return -1;
  260. }
  261. /* *********************************************** */
  262. /*
  263. * This function is called as an URB transfert is complete (Isochronous pipe).
  264. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  265. * and not stall. Neat.
  266. */
  267. static void stk_isoc_handler(struct urb *urb)
  268. {
  269. int i;
  270. int ret;
  271. int framelen;
  272. unsigned long flags;
  273. unsigned char *fill = NULL;
  274. unsigned char *iso_buf = NULL;
  275. struct stk_camera *dev;
  276. struct stk_sio_buffer *fb;
  277. dev = (struct stk_camera *) urb->context;
  278. if (dev == NULL) {
  279. STK_ERROR("isoc_handler called with NULL device !\n");
  280. return;
  281. }
  282. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  283. || urb->status == -ESHUTDOWN) {
  284. atomic_dec(&dev->urbs_used);
  285. return;
  286. }
  287. spin_lock_irqsave(&dev->spinlock, flags);
  288. if (urb->status != -EINPROGRESS && urb->status != 0) {
  289. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  290. goto resubmit;
  291. }
  292. if (list_empty(&dev->sio_avail)) {
  293. /*FIXME Stop streaming after a while */
  294. (void) (printk_ratelimit() &&
  295. STK_ERROR("isoc_handler without available buffer!\n"));
  296. goto resubmit;
  297. }
  298. fb = list_first_entry(&dev->sio_avail,
  299. struct stk_sio_buffer, list);
  300. fill = fb->buffer + fb->v4lbuf.bytesused;
  301. for (i = 0; i < urb->number_of_packets; i++) {
  302. if (urb->iso_frame_desc[i].status != 0) {
  303. if (urb->iso_frame_desc[i].status != -EXDEV)
  304. STK_ERROR("Frame %d has error %d\n", i,
  305. urb->iso_frame_desc[i].status);
  306. continue;
  307. }
  308. framelen = urb->iso_frame_desc[i].actual_length;
  309. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  310. if (framelen <= 4)
  311. continue; /* no data */
  312. /*
  313. * we found something informational from there
  314. * the isoc frames have to type of headers
  315. * type1: 00 xx 00 00 or 20 xx 00 00
  316. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  317. * xx is a sequencer which has never been seen over 0x3f
  318. * imho data written down looks like bayer, i see similarities
  319. * after every 640 bytes
  320. */
  321. if (*iso_buf & 0x80) {
  322. framelen -= 8;
  323. iso_buf += 8;
  324. /* This marks a new frame */
  325. if (fb->v4lbuf.bytesused != 0
  326. && fb->v4lbuf.bytesused != dev->frame_size) {
  327. (void) (printk_ratelimit() &&
  328. STK_ERROR("frame %d, "
  329. "bytesused=%d, skipping\n",
  330. i, fb->v4lbuf.bytesused));
  331. fb->v4lbuf.bytesused = 0;
  332. fill = fb->buffer;
  333. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  334. if (list_is_singular(&dev->sio_avail)) {
  335. /* Always reuse the last buffer */
  336. fb->v4lbuf.bytesused = 0;
  337. fill = fb->buffer;
  338. } else {
  339. list_move_tail(dev->sio_avail.next,
  340. &dev->sio_full);
  341. wake_up(&dev->wait_frame);
  342. fb = list_first_entry(&dev->sio_avail,
  343. struct stk_sio_buffer, list);
  344. fb->v4lbuf.bytesused = 0;
  345. fill = fb->buffer;
  346. }
  347. }
  348. } else {
  349. framelen -= 4;
  350. iso_buf += 4;
  351. }
  352. /* Our buffer is full !!! */
  353. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  354. (void) (printk_ratelimit() &&
  355. STK_ERROR("Frame buffer overflow, lost sync\n"));
  356. /*FIXME Do something here? */
  357. continue;
  358. }
  359. spin_unlock_irqrestore(&dev->spinlock, flags);
  360. memcpy(fill, iso_buf, framelen);
  361. spin_lock_irqsave(&dev->spinlock, flags);
  362. fill += framelen;
  363. /* New size of our buffer */
  364. fb->v4lbuf.bytesused += framelen;
  365. }
  366. resubmit:
  367. spin_unlock_irqrestore(&dev->spinlock, flags);
  368. urb->dev = dev->udev;
  369. ret = usb_submit_urb(urb, GFP_ATOMIC);
  370. if (ret != 0) {
  371. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  372. ret);
  373. }
  374. }
  375. /* -------------------------------------------- */
  376. static int stk_prepare_iso(struct stk_camera *dev)
  377. {
  378. void *kbuf;
  379. int i, j;
  380. struct urb *urb;
  381. struct usb_device *udev;
  382. if (dev == NULL)
  383. return -ENXIO;
  384. udev = dev->udev;
  385. if (dev->isobufs)
  386. STK_ERROR("isobufs already allocated. Bad\n");
  387. else
  388. dev->isobufs = kcalloc(MAX_ISO_BUFS, sizeof(*dev->isobufs),
  389. GFP_KERNEL);
  390. if (dev->isobufs == NULL) {
  391. STK_ERROR("Unable to allocate iso buffers\n");
  392. return -ENOMEM;
  393. }
  394. for (i = 0; i < MAX_ISO_BUFS; i++) {
  395. if (dev->isobufs[i].data == NULL) {
  396. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  397. if (kbuf == NULL) {
  398. STK_ERROR("Failed to allocate iso buffer %d\n",
  399. i);
  400. goto isobufs_out;
  401. }
  402. dev->isobufs[i].data = kbuf;
  403. } else
  404. STK_ERROR("isobuf data already allocated\n");
  405. if (dev->isobufs[i].urb == NULL) {
  406. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  407. if (urb == NULL) {
  408. STK_ERROR("Failed to allocate URB %d\n", i);
  409. goto isobufs_out;
  410. }
  411. dev->isobufs[i].urb = urb;
  412. } else {
  413. STK_ERROR("Killing URB\n");
  414. usb_kill_urb(dev->isobufs[i].urb);
  415. urb = dev->isobufs[i].urb;
  416. }
  417. urb->interval = 1;
  418. urb->dev = udev;
  419. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  420. urb->transfer_flags = URB_ISO_ASAP;
  421. urb->transfer_buffer = dev->isobufs[i].data;
  422. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  423. urb->complete = stk_isoc_handler;
  424. urb->context = dev;
  425. urb->start_frame = 0;
  426. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  427. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  428. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  429. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  430. }
  431. }
  432. set_memallocd(dev);
  433. return 0;
  434. isobufs_out:
  435. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  436. kfree(dev->isobufs[i].data);
  437. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  438. usb_free_urb(dev->isobufs[i].urb);
  439. kfree(dev->isobufs);
  440. dev->isobufs = NULL;
  441. return -ENOMEM;
  442. }
  443. static void stk_clean_iso(struct stk_camera *dev)
  444. {
  445. int i;
  446. if (dev == NULL || dev->isobufs == NULL)
  447. return;
  448. for (i = 0; i < MAX_ISO_BUFS; i++) {
  449. struct urb *urb;
  450. urb = dev->isobufs[i].urb;
  451. if (urb) {
  452. if (atomic_read(&dev->urbs_used) && is_present(dev))
  453. usb_kill_urb(urb);
  454. usb_free_urb(urb);
  455. }
  456. kfree(dev->isobufs[i].data);
  457. }
  458. kfree(dev->isobufs);
  459. dev->isobufs = NULL;
  460. unset_memallocd(dev);
  461. }
  462. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  463. {
  464. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  465. INIT_LIST_HEAD(&buf->list);
  466. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  467. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  468. if (buf->buffer == NULL)
  469. return -ENOMEM;
  470. buf->mapcount = 0;
  471. buf->dev = dev;
  472. buf->v4lbuf.index = index;
  473. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  474. buf->v4lbuf.flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  475. buf->v4lbuf.field = V4L2_FIELD_NONE;
  476. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  477. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  478. return 0;
  479. }
  480. static int stk_free_sio_buffers(struct stk_camera *dev)
  481. {
  482. int i;
  483. int nbufs;
  484. unsigned long flags;
  485. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  486. return 0;
  487. /*
  488. * If any buffers are mapped, we cannot free them at all.
  489. */
  490. for (i = 0; i < dev->n_sbufs; i++) {
  491. if (dev->sio_bufs[i].mapcount > 0)
  492. return -EBUSY;
  493. }
  494. /*
  495. * OK, let's do it.
  496. */
  497. spin_lock_irqsave(&dev->spinlock, flags);
  498. INIT_LIST_HEAD(&dev->sio_avail);
  499. INIT_LIST_HEAD(&dev->sio_full);
  500. nbufs = dev->n_sbufs;
  501. dev->n_sbufs = 0;
  502. spin_unlock_irqrestore(&dev->spinlock, flags);
  503. for (i = 0; i < nbufs; i++)
  504. vfree(dev->sio_bufs[i].buffer);
  505. kfree(dev->sio_bufs);
  506. dev->sio_bufs = NULL;
  507. return 0;
  508. }
  509. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  510. {
  511. int i;
  512. if (dev->sio_bufs != NULL)
  513. STK_ERROR("sio_bufs already allocated\n");
  514. else {
  515. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  516. GFP_KERNEL);
  517. if (dev->sio_bufs == NULL)
  518. return -ENOMEM;
  519. for (i = 0; i < n_sbufs; i++) {
  520. if (stk_setup_siobuf(dev, i))
  521. return (dev->n_sbufs > 1 ? 0 : -ENOMEM);
  522. dev->n_sbufs = i+1;
  523. }
  524. }
  525. return 0;
  526. }
  527. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  528. {
  529. int err;
  530. err = stk_prepare_iso(dev);
  531. if (err) {
  532. stk_clean_iso(dev);
  533. return err;
  534. }
  535. err = stk_prepare_sio_buffers(dev, n_sbufs);
  536. if (err) {
  537. stk_free_sio_buffers(dev);
  538. return err;
  539. }
  540. return 0;
  541. }
  542. static void stk_free_buffers(struct stk_camera *dev)
  543. {
  544. stk_clean_iso(dev);
  545. stk_free_sio_buffers(dev);
  546. }
  547. /* -------------------------------------------- */
  548. /* v4l file operations */
  549. static int v4l_stk_open(struct file *fp)
  550. {
  551. struct stk_camera *dev = video_drvdata(fp);
  552. int err;
  553. if (dev == NULL || !is_present(dev))
  554. return -ENXIO;
  555. if (mutex_lock_interruptible(&dev->lock))
  556. return -ERESTARTSYS;
  557. if (!dev->first_init)
  558. stk_camera_write_reg(dev, 0x0, 0x24);
  559. else
  560. dev->first_init = 0;
  561. err = v4l2_fh_open(fp);
  562. if (!err)
  563. usb_autopm_get_interface(dev->interface);
  564. mutex_unlock(&dev->lock);
  565. return err;
  566. }
  567. static int v4l_stk_release(struct file *fp)
  568. {
  569. struct stk_camera *dev = video_drvdata(fp);
  570. mutex_lock(&dev->lock);
  571. if (dev->owner == fp) {
  572. stk_stop_stream(dev);
  573. stk_free_buffers(dev);
  574. stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
  575. unset_initialised(dev);
  576. dev->owner = NULL;
  577. }
  578. if (is_present(dev))
  579. usb_autopm_put_interface(dev->interface);
  580. mutex_unlock(&dev->lock);
  581. return v4l2_fh_release(fp);
  582. }
  583. static ssize_t stk_read(struct file *fp, char __user *buf,
  584. size_t count, loff_t *f_pos)
  585. {
  586. int i;
  587. int ret;
  588. unsigned long flags;
  589. struct stk_sio_buffer *sbuf;
  590. struct stk_camera *dev = video_drvdata(fp);
  591. if (!is_present(dev))
  592. return -EIO;
  593. if (dev->owner && (!dev->reading || dev->owner != fp))
  594. return -EBUSY;
  595. dev->owner = fp;
  596. if (!is_streaming(dev)) {
  597. if (stk_initialise(dev)
  598. || stk_allocate_buffers(dev, 3)
  599. || stk_start_stream(dev))
  600. return -ENOMEM;
  601. dev->reading = 1;
  602. spin_lock_irqsave(&dev->spinlock, flags);
  603. for (i = 0; i < dev->n_sbufs; i++) {
  604. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  605. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  606. }
  607. spin_unlock_irqrestore(&dev->spinlock, flags);
  608. }
  609. if (*f_pos == 0) {
  610. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  611. return -EWOULDBLOCK;
  612. ret = wait_event_interruptible(dev->wait_frame,
  613. !list_empty(&dev->sio_full) || !is_present(dev));
  614. if (ret)
  615. return ret;
  616. if (!is_present(dev))
  617. return -EIO;
  618. }
  619. if (count + *f_pos > dev->frame_size)
  620. count = dev->frame_size - *f_pos;
  621. spin_lock_irqsave(&dev->spinlock, flags);
  622. if (list_empty(&dev->sio_full)) {
  623. spin_unlock_irqrestore(&dev->spinlock, flags);
  624. STK_ERROR("BUG: No siobufs ready\n");
  625. return 0;
  626. }
  627. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  628. spin_unlock_irqrestore(&dev->spinlock, flags);
  629. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  630. return -EFAULT;
  631. *f_pos += count;
  632. if (*f_pos >= dev->frame_size) {
  633. *f_pos = 0;
  634. spin_lock_irqsave(&dev->spinlock, flags);
  635. list_move_tail(&sbuf->list, &dev->sio_avail);
  636. spin_unlock_irqrestore(&dev->spinlock, flags);
  637. }
  638. return count;
  639. }
  640. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  641. size_t count, loff_t *f_pos)
  642. {
  643. struct stk_camera *dev = video_drvdata(fp);
  644. int ret;
  645. if (mutex_lock_interruptible(&dev->lock))
  646. return -ERESTARTSYS;
  647. ret = stk_read(fp, buf, count, f_pos);
  648. mutex_unlock(&dev->lock);
  649. return ret;
  650. }
  651. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  652. {
  653. struct stk_camera *dev = video_drvdata(fp);
  654. unsigned res = v4l2_ctrl_poll(fp, wait);
  655. poll_wait(fp, &dev->wait_frame, wait);
  656. if (!is_present(dev))
  657. return POLLERR;
  658. if (!list_empty(&dev->sio_full))
  659. return res | POLLIN | POLLRDNORM;
  660. return res;
  661. }
  662. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  663. {
  664. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  665. sbuf->mapcount++;
  666. }
  667. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  668. {
  669. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  670. sbuf->mapcount--;
  671. if (sbuf->mapcount == 0)
  672. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  673. }
  674. static const struct vm_operations_struct stk_v4l_vm_ops = {
  675. .open = stk_v4l_vm_open,
  676. .close = stk_v4l_vm_close
  677. };
  678. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  679. {
  680. unsigned int i;
  681. int ret;
  682. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  683. struct stk_camera *dev = video_drvdata(fp);
  684. struct stk_sio_buffer *sbuf = NULL;
  685. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  686. return -EINVAL;
  687. for (i = 0; i < dev->n_sbufs; i++) {
  688. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  689. sbuf = dev->sio_bufs + i;
  690. break;
  691. }
  692. }
  693. if (sbuf == NULL)
  694. return -EINVAL;
  695. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  696. if (ret)
  697. return ret;
  698. vma->vm_flags |= VM_DONTEXPAND;
  699. vma->vm_private_data = sbuf;
  700. vma->vm_ops = &stk_v4l_vm_ops;
  701. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  702. stk_v4l_vm_open(vma);
  703. return 0;
  704. }
  705. /* v4l ioctl handlers */
  706. static int stk_vidioc_querycap(struct file *filp,
  707. void *priv, struct v4l2_capability *cap)
  708. {
  709. struct stk_camera *dev = video_drvdata(filp);
  710. strcpy(cap->driver, "stk");
  711. strcpy(cap->card, "stk");
  712. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  713. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE
  714. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  715. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  716. return 0;
  717. }
  718. static int stk_vidioc_enum_input(struct file *filp,
  719. void *priv, struct v4l2_input *input)
  720. {
  721. if (input->index != 0)
  722. return -EINVAL;
  723. strcpy(input->name, "Syntek USB Camera");
  724. input->type = V4L2_INPUT_TYPE_CAMERA;
  725. return 0;
  726. }
  727. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  728. {
  729. *i = 0;
  730. return 0;
  731. }
  732. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  733. {
  734. return i ? -EINVAL : 0;
  735. }
  736. static int stk_s_ctrl(struct v4l2_ctrl *ctrl)
  737. {
  738. struct stk_camera *dev =
  739. container_of(ctrl->handler, struct stk_camera, hdl);
  740. switch (ctrl->id) {
  741. case V4L2_CID_BRIGHTNESS:
  742. return stk_sensor_set_brightness(dev, ctrl->val);
  743. case V4L2_CID_HFLIP:
  744. if (dmi_check_system(stk_upside_down_dmi_table))
  745. dev->vsettings.hflip = !ctrl->val;
  746. else
  747. dev->vsettings.hflip = ctrl->val;
  748. return 0;
  749. case V4L2_CID_VFLIP:
  750. if (dmi_check_system(stk_upside_down_dmi_table))
  751. dev->vsettings.vflip = !ctrl->val;
  752. else
  753. dev->vsettings.vflip = ctrl->val;
  754. return 0;
  755. default:
  756. return -EINVAL;
  757. }
  758. return 0;
  759. }
  760. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  761. void *priv, struct v4l2_fmtdesc *fmtd)
  762. {
  763. switch (fmtd->index) {
  764. case 0:
  765. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  766. strcpy(fmtd->description, "r5g6b5");
  767. break;
  768. case 1:
  769. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  770. strcpy(fmtd->description, "r5g6b5BE");
  771. break;
  772. case 2:
  773. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  774. strcpy(fmtd->description, "yuv4:2:2");
  775. break;
  776. case 3:
  777. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  778. strcpy(fmtd->description, "Raw bayer");
  779. break;
  780. case 4:
  781. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  782. strcpy(fmtd->description, "yuv4:2:2");
  783. break;
  784. default:
  785. return -EINVAL;
  786. }
  787. return 0;
  788. }
  789. static struct stk_size {
  790. unsigned w;
  791. unsigned h;
  792. enum stk_mode m;
  793. } stk_sizes[] = {
  794. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  795. { .w = 640, .h = 480, .m = MODE_VGA, },
  796. { .w = 352, .h = 288, .m = MODE_CIF, },
  797. { .w = 320, .h = 240, .m = MODE_QVGA, },
  798. { .w = 176, .h = 144, .m = MODE_QCIF, },
  799. };
  800. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  801. void *priv, struct v4l2_format *f)
  802. {
  803. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  804. struct stk_camera *dev = video_drvdata(filp);
  805. int i;
  806. for (i = 0; i < ARRAY_SIZE(stk_sizes) &&
  807. stk_sizes[i].m != dev->vsettings.mode; i++)
  808. ;
  809. if (i == ARRAY_SIZE(stk_sizes)) {
  810. STK_ERROR("ERROR: mode invalid\n");
  811. return -EINVAL;
  812. }
  813. pix_format->width = stk_sizes[i].w;
  814. pix_format->height = stk_sizes[i].h;
  815. pix_format->field = V4L2_FIELD_NONE;
  816. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  817. pix_format->pixelformat = dev->vsettings.palette;
  818. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  819. pix_format->bytesperline = pix_format->width;
  820. else
  821. pix_format->bytesperline = 2 * pix_format->width;
  822. pix_format->sizeimage = pix_format->bytesperline
  823. * pix_format->height;
  824. return 0;
  825. }
  826. static int stk_try_fmt_vid_cap(struct file *filp,
  827. struct v4l2_format *fmtd, int *idx)
  828. {
  829. int i;
  830. switch (fmtd->fmt.pix.pixelformat) {
  831. case V4L2_PIX_FMT_RGB565:
  832. case V4L2_PIX_FMT_RGB565X:
  833. case V4L2_PIX_FMT_UYVY:
  834. case V4L2_PIX_FMT_YUYV:
  835. case V4L2_PIX_FMT_SBGGR8:
  836. break;
  837. default:
  838. return -EINVAL;
  839. }
  840. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  841. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  842. break;
  843. }
  844. if (i == ARRAY_SIZE(stk_sizes)
  845. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  846. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  847. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  848. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  849. if (idx)
  850. *idx = i - 1;
  851. } else {
  852. fmtd->fmt.pix.height = stk_sizes[i].h;
  853. fmtd->fmt.pix.width = stk_sizes[i].w;
  854. if (idx)
  855. *idx = i;
  856. }
  857. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  858. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  859. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  860. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  861. else
  862. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  863. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  864. * fmtd->fmt.pix.height;
  865. return 0;
  866. }
  867. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  868. void *priv, struct v4l2_format *fmtd)
  869. {
  870. return stk_try_fmt_vid_cap(filp, fmtd, NULL);
  871. }
  872. static int stk_setup_format(struct stk_camera *dev)
  873. {
  874. int i = 0;
  875. int depth;
  876. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  877. depth = 1;
  878. else
  879. depth = 2;
  880. while (i < ARRAY_SIZE(stk_sizes) &&
  881. stk_sizes[i].m != dev->vsettings.mode)
  882. i++;
  883. if (i == ARRAY_SIZE(stk_sizes)) {
  884. STK_ERROR("Something is broken in %s\n", __func__);
  885. return -EFAULT;
  886. }
  887. /* This registers controls some timings, not sure of what. */
  888. stk_camera_write_reg(dev, 0x001b, 0x0e);
  889. if (dev->vsettings.mode == MODE_SXGA)
  890. stk_camera_write_reg(dev, 0x001c, 0x0e);
  891. else
  892. stk_camera_write_reg(dev, 0x001c, 0x46);
  893. /*
  894. * Registers 0x0115 0x0114 are the size of each line (bytes),
  895. * regs 0x0117 0x0116 are the heigth of the image.
  896. */
  897. stk_camera_write_reg(dev, 0x0115,
  898. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  899. stk_camera_write_reg(dev, 0x0114,
  900. (stk_sizes[i].w * depth) & 0xff);
  901. stk_camera_write_reg(dev, 0x0117,
  902. (stk_sizes[i].h >> 8) & 0xff);
  903. stk_camera_write_reg(dev, 0x0116,
  904. stk_sizes[i].h & 0xff);
  905. return stk_sensor_configure(dev);
  906. }
  907. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  908. void *priv, struct v4l2_format *fmtd)
  909. {
  910. int ret;
  911. int idx;
  912. struct stk_camera *dev = video_drvdata(filp);
  913. if (dev == NULL)
  914. return -ENODEV;
  915. if (!is_present(dev))
  916. return -ENODEV;
  917. if (is_streaming(dev))
  918. return -EBUSY;
  919. if (dev->owner)
  920. return -EBUSY;
  921. ret = stk_try_fmt_vid_cap(filp, fmtd, &idx);
  922. if (ret)
  923. return ret;
  924. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  925. stk_free_buffers(dev);
  926. dev->frame_size = fmtd->fmt.pix.sizeimage;
  927. dev->vsettings.mode = stk_sizes[idx].m;
  928. stk_initialise(dev);
  929. return stk_setup_format(dev);
  930. }
  931. static int stk_vidioc_reqbufs(struct file *filp,
  932. void *priv, struct v4l2_requestbuffers *rb)
  933. {
  934. struct stk_camera *dev = video_drvdata(filp);
  935. if (dev == NULL)
  936. return -ENODEV;
  937. if (rb->memory != V4L2_MEMORY_MMAP)
  938. return -EINVAL;
  939. if (is_streaming(dev)
  940. || (dev->owner && dev->owner != filp))
  941. return -EBUSY;
  942. stk_free_buffers(dev);
  943. if (rb->count == 0) {
  944. stk_camera_write_reg(dev, 0x0, 0x49); /* turn off the LED */
  945. unset_initialised(dev);
  946. dev->owner = NULL;
  947. return 0;
  948. }
  949. dev->owner = filp;
  950. /*FIXME If they ask for zero, we must stop streaming and free */
  951. if (rb->count < 3)
  952. rb->count = 3;
  953. /* Arbitrary limit */
  954. else if (rb->count > 5)
  955. rb->count = 5;
  956. stk_allocate_buffers(dev, rb->count);
  957. rb->count = dev->n_sbufs;
  958. return 0;
  959. }
  960. static int stk_vidioc_querybuf(struct file *filp,
  961. void *priv, struct v4l2_buffer *buf)
  962. {
  963. struct stk_camera *dev = video_drvdata(filp);
  964. struct stk_sio_buffer *sbuf;
  965. if (buf->index >= dev->n_sbufs)
  966. return -EINVAL;
  967. sbuf = dev->sio_bufs + buf->index;
  968. *buf = sbuf->v4lbuf;
  969. return 0;
  970. }
  971. static int stk_vidioc_qbuf(struct file *filp,
  972. void *priv, struct v4l2_buffer *buf)
  973. {
  974. struct stk_camera *dev = video_drvdata(filp);
  975. struct stk_sio_buffer *sbuf;
  976. unsigned long flags;
  977. if (buf->memory != V4L2_MEMORY_MMAP)
  978. return -EINVAL;
  979. if (buf->index >= dev->n_sbufs)
  980. return -EINVAL;
  981. sbuf = dev->sio_bufs + buf->index;
  982. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  983. return 0;
  984. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  985. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  986. spin_lock_irqsave(&dev->spinlock, flags);
  987. list_add_tail(&sbuf->list, &dev->sio_avail);
  988. *buf = sbuf->v4lbuf;
  989. spin_unlock_irqrestore(&dev->spinlock, flags);
  990. return 0;
  991. }
  992. static int stk_vidioc_dqbuf(struct file *filp,
  993. void *priv, struct v4l2_buffer *buf)
  994. {
  995. struct stk_camera *dev = video_drvdata(filp);
  996. struct stk_sio_buffer *sbuf;
  997. unsigned long flags;
  998. int ret;
  999. if (!is_streaming(dev))
  1000. return -EINVAL;
  1001. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  1002. return -EWOULDBLOCK;
  1003. ret = wait_event_interruptible(dev->wait_frame,
  1004. !list_empty(&dev->sio_full) || !is_present(dev));
  1005. if (ret)
  1006. return ret;
  1007. if (!is_present(dev))
  1008. return -EIO;
  1009. spin_lock_irqsave(&dev->spinlock, flags);
  1010. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  1011. list_del_init(&sbuf->list);
  1012. spin_unlock_irqrestore(&dev->spinlock, flags);
  1013. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  1014. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  1015. sbuf->v4lbuf.sequence = ++dev->sequence;
  1016. v4l2_get_timestamp(&sbuf->v4lbuf.timestamp);
  1017. *buf = sbuf->v4lbuf;
  1018. return 0;
  1019. }
  1020. static int stk_vidioc_streamon(struct file *filp,
  1021. void *priv, enum v4l2_buf_type type)
  1022. {
  1023. struct stk_camera *dev = video_drvdata(filp);
  1024. if (is_streaming(dev))
  1025. return 0;
  1026. if (dev->sio_bufs == NULL)
  1027. return -EINVAL;
  1028. dev->sequence = 0;
  1029. return stk_start_stream(dev);
  1030. }
  1031. static int stk_vidioc_streamoff(struct file *filp,
  1032. void *priv, enum v4l2_buf_type type)
  1033. {
  1034. struct stk_camera *dev = video_drvdata(filp);
  1035. unsigned long flags;
  1036. int i;
  1037. stk_stop_stream(dev);
  1038. spin_lock_irqsave(&dev->spinlock, flags);
  1039. INIT_LIST_HEAD(&dev->sio_avail);
  1040. INIT_LIST_HEAD(&dev->sio_full);
  1041. for (i = 0; i < dev->n_sbufs; i++) {
  1042. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1043. dev->sio_bufs[i].v4lbuf.flags = 0;
  1044. }
  1045. spin_unlock_irqrestore(&dev->spinlock, flags);
  1046. return 0;
  1047. }
  1048. static int stk_vidioc_g_parm(struct file *filp,
  1049. void *priv, struct v4l2_streamparm *sp)
  1050. {
  1051. /*FIXME This is not correct */
  1052. sp->parm.capture.timeperframe.numerator = 1;
  1053. sp->parm.capture.timeperframe.denominator = 30;
  1054. sp->parm.capture.readbuffers = 2;
  1055. return 0;
  1056. }
  1057. static int stk_vidioc_enum_framesizes(struct file *filp,
  1058. void *priv, struct v4l2_frmsizeenum *frms)
  1059. {
  1060. if (frms->index >= ARRAY_SIZE(stk_sizes))
  1061. return -EINVAL;
  1062. switch (frms->pixel_format) {
  1063. case V4L2_PIX_FMT_RGB565:
  1064. case V4L2_PIX_FMT_RGB565X:
  1065. case V4L2_PIX_FMT_UYVY:
  1066. case V4L2_PIX_FMT_YUYV:
  1067. case V4L2_PIX_FMT_SBGGR8:
  1068. frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1069. frms->discrete.width = stk_sizes[frms->index].w;
  1070. frms->discrete.height = stk_sizes[frms->index].h;
  1071. return 0;
  1072. default: return -EINVAL;
  1073. }
  1074. }
  1075. static const struct v4l2_ctrl_ops stk_ctrl_ops = {
  1076. .s_ctrl = stk_s_ctrl,
  1077. };
  1078. static struct v4l2_file_operations v4l_stk_fops = {
  1079. .owner = THIS_MODULE,
  1080. .open = v4l_stk_open,
  1081. .release = v4l_stk_release,
  1082. .read = v4l_stk_read,
  1083. .poll = v4l_stk_poll,
  1084. .mmap = v4l_stk_mmap,
  1085. .unlocked_ioctl = video_ioctl2,
  1086. };
  1087. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1088. .vidioc_querycap = stk_vidioc_querycap,
  1089. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1090. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1091. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1092. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1093. .vidioc_enum_input = stk_vidioc_enum_input,
  1094. .vidioc_s_input = stk_vidioc_s_input,
  1095. .vidioc_g_input = stk_vidioc_g_input,
  1096. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1097. .vidioc_querybuf = stk_vidioc_querybuf,
  1098. .vidioc_qbuf = stk_vidioc_qbuf,
  1099. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1100. .vidioc_streamon = stk_vidioc_streamon,
  1101. .vidioc_streamoff = stk_vidioc_streamoff,
  1102. .vidioc_g_parm = stk_vidioc_g_parm,
  1103. .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
  1104. .vidioc_log_status = v4l2_ctrl_log_status,
  1105. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1106. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1107. };
  1108. static void stk_v4l_dev_release(struct video_device *vd)
  1109. {
  1110. struct stk_camera *dev = vdev_to_camera(vd);
  1111. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  1112. STK_ERROR("We are leaking memory\n");
  1113. usb_put_intf(dev->interface);
  1114. kfree(dev);
  1115. }
  1116. static struct video_device stk_v4l_data = {
  1117. .name = "stkwebcam",
  1118. .fops = &v4l_stk_fops,
  1119. .ioctl_ops = &v4l_stk_ioctl_ops,
  1120. .release = stk_v4l_dev_release,
  1121. };
  1122. static int stk_register_video_device(struct stk_camera *dev)
  1123. {
  1124. int err;
  1125. dev->vdev = stk_v4l_data;
  1126. dev->vdev.lock = &dev->lock;
  1127. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1128. video_set_drvdata(&dev->vdev, dev);
  1129. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1130. if (err)
  1131. STK_ERROR("v4l registration failed\n");
  1132. else
  1133. STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
  1134. video_device_node_name(&dev->vdev));
  1135. return err;
  1136. }
  1137. /* USB Stuff */
  1138. static int stk_camera_probe(struct usb_interface *interface,
  1139. const struct usb_device_id *id)
  1140. {
  1141. struct v4l2_ctrl_handler *hdl;
  1142. int err = 0;
  1143. int i;
  1144. struct stk_camera *dev = NULL;
  1145. struct usb_device *udev = interface_to_usbdev(interface);
  1146. struct usb_host_interface *iface_desc;
  1147. struct usb_endpoint_descriptor *endpoint;
  1148. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1149. if (dev == NULL) {
  1150. STK_ERROR("Out of memory !\n");
  1151. return -ENOMEM;
  1152. }
  1153. err = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  1154. if (err < 0) {
  1155. dev_err(&udev->dev, "couldn't register v4l2_device\n");
  1156. kfree(dev);
  1157. return err;
  1158. }
  1159. hdl = &dev->hdl;
  1160. v4l2_ctrl_handler_init(hdl, 3);
  1161. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1162. V4L2_CID_BRIGHTNESS, 0, 0xff, 0x1, 0x60);
  1163. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1164. V4L2_CID_HFLIP, 0, 1, 1, 1);
  1165. v4l2_ctrl_new_std(hdl, &stk_ctrl_ops,
  1166. V4L2_CID_VFLIP, 0, 1, 1, 1);
  1167. if (hdl->error) {
  1168. err = hdl->error;
  1169. dev_err(&udev->dev, "couldn't register control\n");
  1170. goto error;
  1171. }
  1172. dev->v4l2_dev.ctrl_handler = hdl;
  1173. spin_lock_init(&dev->spinlock);
  1174. mutex_init(&dev->lock);
  1175. init_waitqueue_head(&dev->wait_frame);
  1176. dev->first_init = 1; /* webcam LED management */
  1177. dev->udev = udev;
  1178. dev->interface = interface;
  1179. usb_get_intf(interface);
  1180. if (hflip != -1)
  1181. dev->vsettings.hflip = hflip;
  1182. else if (dmi_check_system(stk_upside_down_dmi_table))
  1183. dev->vsettings.hflip = 1;
  1184. else
  1185. dev->vsettings.hflip = 0;
  1186. if (vflip != -1)
  1187. dev->vsettings.vflip = vflip;
  1188. else if (dmi_check_system(stk_upside_down_dmi_table))
  1189. dev->vsettings.vflip = 1;
  1190. else
  1191. dev->vsettings.vflip = 0;
  1192. dev->n_sbufs = 0;
  1193. set_present(dev);
  1194. /* Set up the endpoint information
  1195. * use only the first isoc-in endpoint
  1196. * for the current alternate setting */
  1197. iface_desc = interface->cur_altsetting;
  1198. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1199. endpoint = &iface_desc->endpoint[i].desc;
  1200. if (!dev->isoc_ep
  1201. && usb_endpoint_is_isoc_in(endpoint)) {
  1202. /* we found an isoc in endpoint */
  1203. dev->isoc_ep = usb_endpoint_num(endpoint);
  1204. break;
  1205. }
  1206. }
  1207. if (!dev->isoc_ep) {
  1208. STK_ERROR("Could not find isoc-in endpoint");
  1209. err = -ENODEV;
  1210. goto error;
  1211. }
  1212. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1213. dev->vsettings.mode = MODE_VGA;
  1214. dev->frame_size = 640 * 480 * 2;
  1215. INIT_LIST_HEAD(&dev->sio_avail);
  1216. INIT_LIST_HEAD(&dev->sio_full);
  1217. usb_set_intfdata(interface, dev);
  1218. err = stk_register_video_device(dev);
  1219. if (err)
  1220. goto error;
  1221. return 0;
  1222. error:
  1223. v4l2_ctrl_handler_free(hdl);
  1224. v4l2_device_unregister(&dev->v4l2_dev);
  1225. kfree(dev);
  1226. return err;
  1227. }
  1228. static void stk_camera_disconnect(struct usb_interface *interface)
  1229. {
  1230. struct stk_camera *dev = usb_get_intfdata(interface);
  1231. usb_set_intfdata(interface, NULL);
  1232. unset_present(dev);
  1233. wake_up_interruptible(&dev->wait_frame);
  1234. STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
  1235. video_device_node_name(&dev->vdev));
  1236. video_unregister_device(&dev->vdev);
  1237. v4l2_ctrl_handler_free(&dev->hdl);
  1238. v4l2_device_unregister(&dev->v4l2_dev);
  1239. }
  1240. #ifdef CONFIG_PM
  1241. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1242. {
  1243. struct stk_camera *dev = usb_get_intfdata(intf);
  1244. if (is_streaming(dev)) {
  1245. stk_stop_stream(dev);
  1246. /* yes, this is ugly */
  1247. set_streaming(dev);
  1248. }
  1249. return 0;
  1250. }
  1251. static int stk_camera_resume(struct usb_interface *intf)
  1252. {
  1253. struct stk_camera *dev = usb_get_intfdata(intf);
  1254. if (!is_initialised(dev))
  1255. return 0;
  1256. unset_initialised(dev);
  1257. stk_initialise(dev);
  1258. stk_camera_write_reg(dev, 0x0, 0x49);
  1259. stk_setup_format(dev);
  1260. if (is_streaming(dev))
  1261. stk_start_stream(dev);
  1262. return 0;
  1263. }
  1264. #endif
  1265. static struct usb_driver stk_camera_driver = {
  1266. .name = "stkwebcam",
  1267. .probe = stk_camera_probe,
  1268. .disconnect = stk_camera_disconnect,
  1269. .id_table = stkwebcam_table,
  1270. #ifdef CONFIG_PM
  1271. .suspend = stk_camera_suspend,
  1272. .resume = stk_camera_resume,
  1273. #endif
  1274. };
  1275. module_usb_driver(stk_camera_driver);