stk-webcam.c 35 KB

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