konicawc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * konicawc.c - konica webcam driver
  3. *
  4. * Author: Simon Evans <spse@secret.org.uk>
  5. *
  6. * Copyright (C) 2002 Simon Evans
  7. *
  8. * Licence: GPL
  9. *
  10. * Driver for USB webcams based on Konica chipset. This
  11. * chipset is used in Intel YC76 camera.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/usb/input.h>
  18. #include "usbvideo.h"
  19. #define MAX_BRIGHTNESS 108
  20. #define MAX_CONTRAST 108
  21. #define MAX_SATURATION 108
  22. #define MAX_SHARPNESS 108
  23. #define MAX_WHITEBAL 372
  24. #define MAX_SPEED 6
  25. #define MAX_CAMERAS 1
  26. #define DRIVER_VERSION "v1.4"
  27. #define DRIVER_DESC "Konica Webcam driver"
  28. enum ctrl_req {
  29. SetWhitebal = 0x01,
  30. SetBrightness = 0x02,
  31. SetSharpness = 0x03,
  32. SetContrast = 0x04,
  33. SetSaturation = 0x05,
  34. };
  35. enum frame_sizes {
  36. SIZE_160X120 = 0,
  37. SIZE_160X136 = 1,
  38. SIZE_176X144 = 2,
  39. SIZE_320X240 = 3,
  40. };
  41. #define MAX_FRAME_SIZE SIZE_320X240
  42. static struct usbvideo *cams;
  43. #ifdef CONFIG_USB_DEBUG
  44. static int debug;
  45. #define DEBUG(n, format, arg...) \
  46. if (n <= debug) { \
  47. printk(KERN_DEBUG __FILE__ ":%s(): " format "\n", __FUNCTION__ , ## arg); \
  48. }
  49. #else
  50. #define DEBUG(n, arg...)
  51. static const int debug = 0;
  52. #endif
  53. /* Some default values for initial camera settings,
  54. can be set by modprobe */
  55. static int size;
  56. static int speed = 6; /* Speed (fps) 0 (slowest) to 6 (fastest) */
  57. static int brightness = MAX_BRIGHTNESS/2;
  58. static int contrast = MAX_CONTRAST/2;
  59. static int saturation = MAX_SATURATION/2;
  60. static int sharpness = MAX_SHARPNESS/2;
  61. static int whitebal = 3*(MAX_WHITEBAL/4);
  62. static const int spd_to_iface[] = { 1, 0, 3, 2, 4, 5, 6 };
  63. /* These FPS speeds are from the windows config box. They are
  64. * indexed on size (0-2) and speed (0-6). Divide by 3 to get the
  65. * real fps.
  66. */
  67. static const int spd_to_fps[][7] = { { 24, 40, 48, 60, 72, 80, 100 },
  68. { 24, 40, 48, 60, 72, 80, 100 },
  69. { 18, 30, 36, 45, 54, 60, 75 },
  70. { 6, 10, 12, 15, 18, 21, 25 } };
  71. struct cam_size {
  72. u16 width;
  73. u16 height;
  74. u8 cmd;
  75. };
  76. static const struct cam_size camera_sizes[] = { { 160, 120, 0x7 },
  77. { 160, 136, 0xa },
  78. { 176, 144, 0x4 },
  79. { 320, 240, 0x5 } };
  80. struct konicawc {
  81. u8 brightness; /* camera uses 0 - 9, x11 for real value */
  82. u8 contrast; /* as above */
  83. u8 saturation; /* as above */
  84. u8 sharpness; /* as above */
  85. u8 white_bal; /* 0 - 33, x11 for real value */
  86. u8 speed; /* Stored as 0 - 6, used as index in spd_to_* (above) */
  87. u8 size; /* Frame Size */
  88. int height;
  89. int width;
  90. struct urb *sts_urb[USBVIDEO_NUMSBUF];
  91. u8 sts_buf[USBVIDEO_NUMSBUF][FRAMES_PER_DESC];
  92. struct urb *last_data_urb;
  93. int lastframe;
  94. int cur_frame_size; /* number of bytes in current frame size */
  95. int maxline; /* number of lines per frame */
  96. int yplanesz; /* Number of bytes in the Y plane */
  97. unsigned int buttonsts:1;
  98. #ifdef CONFIG_INPUT
  99. struct input_dev *input;
  100. char input_physname[64];
  101. #endif
  102. };
  103. #define konicawc_set_misc(uvd, req, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, req, value, index, NULL, 0)
  104. #define konicawc_get_misc(uvd, req, value, index, buf, sz) konicawc_ctrl_msg(uvd, USB_DIR_IN, req, value, index, buf, sz)
  105. #define konicawc_set_value(uvd, value, index) konicawc_ctrl_msg(uvd, USB_DIR_OUT, 2, value, index, NULL, 0)
  106. static int konicawc_ctrl_msg(struct uvd *uvd, u8 dir, u8 request, u16 value, u16 index, void *buf, int len)
  107. {
  108. int retval = usb_control_msg(uvd->dev,
  109. dir ? usb_rcvctrlpipe(uvd->dev, 0) : usb_sndctrlpipe(uvd->dev, 0),
  110. request, 0x40 | dir, value, index, buf, len, 1000);
  111. return retval < 0 ? retval : 0;
  112. }
  113. static inline void konicawc_camera_on(struct uvd *uvd)
  114. {
  115. DEBUG(0, "camera on");
  116. konicawc_set_misc(uvd, 0x2, 1, 0x0b);
  117. }
  118. static inline void konicawc_camera_off(struct uvd *uvd)
  119. {
  120. DEBUG(0, "camera off");
  121. konicawc_set_misc(uvd, 0x2, 0, 0x0b);
  122. }
  123. static void konicawc_set_camera_size(struct uvd *uvd)
  124. {
  125. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  126. konicawc_set_misc(uvd, 0x2, camera_sizes[cam->size].cmd, 0x08);
  127. cam->width = camera_sizes[cam->size].width;
  128. cam->height = camera_sizes[cam->size].height;
  129. cam->yplanesz = cam->height * cam->width;
  130. cam->cur_frame_size = (cam->yplanesz * 3) / 2;
  131. cam->maxline = cam->yplanesz / 256;
  132. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  133. }
  134. static int konicawc_setup_on_open(struct uvd *uvd)
  135. {
  136. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  137. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  138. cam->brightness * 11);
  139. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  140. DEBUG(1, "setting white balance to %d (%d)", cam->white_bal,
  141. cam->white_bal * 11);
  142. konicawc_set_value(uvd, cam->white_bal, SetWhitebal);
  143. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  144. cam->contrast * 11);
  145. konicawc_set_value(uvd, cam->contrast, SetContrast);
  146. DEBUG(1, "setting saturation to %d (%d)", cam->saturation,
  147. cam->saturation * 11);
  148. konicawc_set_value(uvd, cam->saturation, SetSaturation);
  149. DEBUG(1, "setting sharpness to %d (%d)", cam->sharpness,
  150. cam->sharpness * 11);
  151. konicawc_set_value(uvd, cam->sharpness, SetSharpness);
  152. konicawc_set_camera_size(uvd);
  153. cam->lastframe = -2;
  154. cam->buttonsts = 0;
  155. return 0;
  156. }
  157. static void konicawc_adjust_picture(struct uvd *uvd)
  158. {
  159. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  160. konicawc_camera_off(uvd);
  161. DEBUG(1, "new brightness: %d", uvd->vpic.brightness);
  162. uvd->vpic.brightness = (uvd->vpic.brightness > MAX_BRIGHTNESS) ? MAX_BRIGHTNESS : uvd->vpic.brightness;
  163. if(cam->brightness != uvd->vpic.brightness / 11) {
  164. cam->brightness = uvd->vpic.brightness / 11;
  165. DEBUG(1, "setting brightness to %d (%d)", cam->brightness,
  166. cam->brightness * 11);
  167. konicawc_set_value(uvd, cam->brightness, SetBrightness);
  168. }
  169. DEBUG(1, "new contrast: %d", uvd->vpic.contrast);
  170. uvd->vpic.contrast = (uvd->vpic.contrast > MAX_CONTRAST) ? MAX_CONTRAST : uvd->vpic.contrast;
  171. if(cam->contrast != uvd->vpic.contrast / 11) {
  172. cam->contrast = uvd->vpic.contrast / 11;
  173. DEBUG(1, "setting contrast to %d (%d)", cam->contrast,
  174. cam->contrast * 11);
  175. konicawc_set_value(uvd, cam->contrast, SetContrast);
  176. }
  177. konicawc_camera_on(uvd);
  178. }
  179. #ifdef CONFIG_INPUT
  180. static void konicawc_register_input(struct konicawc *cam, struct usb_device *dev)
  181. {
  182. struct input_dev *input_dev;
  183. usb_make_path(dev, cam->input_physname, sizeof(cam->input_physname));
  184. strncat(cam->input_physname, "/input0", sizeof(cam->input_physname));
  185. cam->input = input_dev = input_allocate_device();
  186. if (!input_dev) {
  187. warn("Not enough memory for camera's input device\n");
  188. return;
  189. }
  190. input_dev->name = "Konicawc snapshot button";
  191. input_dev->phys = cam->input_physname;
  192. usb_to_input_id(dev, &input_dev->id);
  193. input_dev->cdev.dev = &dev->dev;
  194. input_dev->evbit[0] = BIT(EV_KEY);
  195. input_dev->keybit[LONG(BTN_0)] = BIT(BTN_0);
  196. input_dev->private = cam;
  197. input_register_device(cam->input);
  198. }
  199. static void konicawc_unregister_input(struct konicawc *cam)
  200. {
  201. if (cam->input) {
  202. input_unregister_device(cam->input);
  203. cam->input = NULL;
  204. }
  205. }
  206. static void konicawc_report_buttonstat(struct konicawc *cam)
  207. {
  208. if (cam->input) {
  209. input_report_key(cam->input, BTN_0, cam->buttonsts);
  210. input_sync(cam->input);
  211. }
  212. }
  213. #else
  214. static inline void konicawc_register_input(struct konicawc *cam, struct usb_device *dev) { }
  215. static inline void konicawc_unregister_input(struct konicawc *cam) { }
  216. static inline void konicawc_report_buttonstat(struct konicawc *cam) { }
  217. #endif /* CONFIG_INPUT */
  218. static int konicawc_compress_iso(struct uvd *uvd, struct urb *dataurb, struct urb *stsurb)
  219. {
  220. char *cdata;
  221. int i, totlen = 0;
  222. unsigned char *status = stsurb->transfer_buffer;
  223. int keep = 0, discard = 0, bad = 0;
  224. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  225. for (i = 0; i < dataurb->number_of_packets; i++) {
  226. int button = cam->buttonsts;
  227. unsigned char sts;
  228. int n = dataurb->iso_frame_desc[i].actual_length;
  229. int st = dataurb->iso_frame_desc[i].status;
  230. cdata = dataurb->transfer_buffer +
  231. dataurb->iso_frame_desc[i].offset;
  232. /* Detect and ignore errored packets */
  233. if (st < 0) {
  234. DEBUG(1, "Data error: packet=%d. len=%d. status=%d.",
  235. i, n, st);
  236. uvd->stats.iso_err_count++;
  237. continue;
  238. }
  239. /* Detect and ignore empty packets */
  240. if (n <= 0) {
  241. uvd->stats.iso_skip_count++;
  242. continue;
  243. }
  244. /* See what the status data said about the packet */
  245. sts = *(status+stsurb->iso_frame_desc[i].offset);
  246. /* sts: 0x80-0xff: frame start with frame number (ie 0-7f)
  247. * otherwise:
  248. * bit 0 0: keep packet
  249. * 1: drop packet (padding data)
  250. *
  251. * bit 4 0 button not clicked
  252. * 1 button clicked
  253. * button is used to `take a picture' (in software)
  254. */
  255. if(sts < 0x80) {
  256. button = !!(sts & 0x40);
  257. sts &= ~0x40;
  258. }
  259. /* work out the button status, but don't do
  260. anything with it for now */
  261. if(button != cam->buttonsts) {
  262. DEBUG(2, "button: %sclicked", button ? "" : "un");
  263. cam->buttonsts = button;
  264. konicawc_report_buttonstat(cam);
  265. }
  266. if(sts == 0x01) { /* drop frame */
  267. discard++;
  268. continue;
  269. }
  270. if((sts > 0x01) && (sts < 0x80)) {
  271. info("unknown status %2.2x", sts);
  272. bad++;
  273. continue;
  274. }
  275. if(!sts && cam->lastframe == -2) {
  276. DEBUG(2, "dropping frame looking for image start");
  277. continue;
  278. }
  279. keep++;
  280. if(sts & 0x80) { /* frame start */
  281. unsigned char marker[] = { 0, 0xff, 0, 0x00 };
  282. if(cam->lastframe == -2) {
  283. DEBUG(2, "found initial image");
  284. cam->lastframe = -1;
  285. }
  286. marker[3] = sts & 0x7F;
  287. RingQueue_Enqueue(&uvd->dp, marker, 4);
  288. totlen += 4;
  289. }
  290. totlen += n; /* Little local accounting */
  291. RingQueue_Enqueue(&uvd->dp, cdata, n);
  292. }
  293. DEBUG(8, "finished: keep = %d discard = %d bad = %d added %d bytes",
  294. keep, discard, bad, totlen);
  295. return totlen;
  296. }
  297. static void resubmit_urb(struct uvd *uvd, struct urb *urb)
  298. {
  299. int i, ret;
  300. for (i = 0; i < FRAMES_PER_DESC; i++) {
  301. urb->iso_frame_desc[i].status = 0;
  302. }
  303. urb->dev = uvd->dev;
  304. urb->status = 0;
  305. ret = usb_submit_urb(urb, GFP_ATOMIC);
  306. DEBUG(3, "submitting urb of length %d", urb->transfer_buffer_length);
  307. if(ret)
  308. err("usb_submit_urb error (%d)", ret);
  309. }
  310. static void konicawc_isoc_irq(struct urb *urb, struct pt_regs *regs)
  311. {
  312. struct uvd *uvd = urb->context;
  313. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  314. /* We don't want to do anything if we are about to be removed! */
  315. if (!CAMERA_IS_OPERATIONAL(uvd))
  316. return;
  317. if (!uvd->streaming) {
  318. DEBUG(1, "Not streaming, but interrupt!");
  319. return;
  320. }
  321. DEBUG(3, "got frame %d len = %d buflen =%d", urb->start_frame, urb->actual_length, urb->transfer_buffer_length);
  322. uvd->stats.urb_count++;
  323. if (urb->transfer_buffer_length > 32) {
  324. cam->last_data_urb = urb;
  325. return;
  326. }
  327. /* Copy the data received into ring queue */
  328. if(cam->last_data_urb) {
  329. int len = 0;
  330. if(urb->start_frame != cam->last_data_urb->start_frame)
  331. err("Lost sync on frames");
  332. else if (!urb->status && !cam->last_data_urb->status)
  333. len = konicawc_compress_iso(uvd, cam->last_data_urb, urb);
  334. resubmit_urb(uvd, cam->last_data_urb);
  335. resubmit_urb(uvd, urb);
  336. cam->last_data_urb = NULL;
  337. uvd->stats.urb_length = len;
  338. uvd->stats.data_count += len;
  339. if(len)
  340. RingQueue_WakeUpInterruptible(&uvd->dp);
  341. return;
  342. }
  343. return;
  344. }
  345. static int konicawc_start_data(struct uvd *uvd)
  346. {
  347. struct usb_device *dev = uvd->dev;
  348. int i, errFlag;
  349. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  350. int pktsz;
  351. struct usb_interface *intf;
  352. struct usb_host_interface *interface = NULL;
  353. intf = usb_ifnum_to_if(dev, uvd->iface);
  354. if (intf)
  355. interface = usb_altnum_to_altsetting(intf,
  356. spd_to_iface[cam->speed]);
  357. if (!interface)
  358. return -ENXIO;
  359. pktsz = le16_to_cpu(interface->endpoint[1].desc.wMaxPacketSize);
  360. DEBUG(1, "pktsz = %d", pktsz);
  361. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  362. err("Camera is not operational");
  363. return -EFAULT;
  364. }
  365. uvd->curframe = -1;
  366. konicawc_camera_on(uvd);
  367. /* Alternate interface 1 is is the biggest frame size */
  368. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  369. if (i < 0) {
  370. err("usb_set_interface error");
  371. uvd->last_error = i;
  372. return -EBUSY;
  373. }
  374. /* We double buffer the Iso lists */
  375. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  376. int j, k;
  377. struct urb *urb = uvd->sbuf[i].urb;
  378. urb->dev = dev;
  379. urb->context = uvd;
  380. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  381. urb->interval = 1;
  382. urb->transfer_flags = URB_ISO_ASAP;
  383. urb->transfer_buffer = uvd->sbuf[i].data;
  384. urb->complete = konicawc_isoc_irq;
  385. urb->number_of_packets = FRAMES_PER_DESC;
  386. urb->transfer_buffer_length = pktsz * FRAMES_PER_DESC;
  387. for (j=k=0; j < FRAMES_PER_DESC; j++, k += pktsz) {
  388. urb->iso_frame_desc[j].offset = k;
  389. urb->iso_frame_desc[j].length = pktsz;
  390. }
  391. urb = cam->sts_urb[i];
  392. urb->dev = dev;
  393. urb->context = uvd;
  394. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp-1);
  395. urb->interval = 1;
  396. urb->transfer_flags = URB_ISO_ASAP;
  397. urb->transfer_buffer = cam->sts_buf[i];
  398. urb->complete = konicawc_isoc_irq;
  399. urb->number_of_packets = FRAMES_PER_DESC;
  400. urb->transfer_buffer_length = FRAMES_PER_DESC;
  401. for (j=0; j < FRAMES_PER_DESC; j++) {
  402. urb->iso_frame_desc[j].offset = j;
  403. urb->iso_frame_desc[j].length = 1;
  404. }
  405. }
  406. cam->last_data_urb = NULL;
  407. /* Submit all URBs */
  408. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  409. errFlag = usb_submit_urb(cam->sts_urb[i], GFP_KERNEL);
  410. if (errFlag)
  411. err("usb_submit_isoc(%d) ret %d", i, errFlag);
  412. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  413. if (errFlag)
  414. err ("usb_submit_isoc(%d) ret %d", i, errFlag);
  415. }
  416. uvd->streaming = 1;
  417. DEBUG(1, "streaming=1 video_endp=$%02x", uvd->video_endp);
  418. return 0;
  419. }
  420. static void konicawc_stop_data(struct uvd *uvd)
  421. {
  422. int i, j;
  423. struct konicawc *cam;
  424. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  425. return;
  426. konicawc_camera_off(uvd);
  427. uvd->streaming = 0;
  428. cam = (struct konicawc *)uvd->user_data;
  429. cam->last_data_urb = NULL;
  430. /* Unschedule all of the iso td's */
  431. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  432. usb_kill_urb(uvd->sbuf[i].urb);
  433. usb_kill_urb(cam->sts_urb[i]);
  434. }
  435. if (!uvd->remove_pending) {
  436. /* Set packet size to 0 */
  437. j = usb_set_interface(uvd->dev, uvd->iface, uvd->ifaceAltInactive);
  438. if (j < 0) {
  439. err("usb_set_interface() error %d.", j);
  440. uvd->last_error = j;
  441. }
  442. }
  443. }
  444. static void konicawc_process_isoc(struct uvd *uvd, struct usbvideo_frame *frame)
  445. {
  446. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  447. int maxline = cam->maxline;
  448. int yplanesz = cam->yplanesz;
  449. assert(frame != NULL);
  450. DEBUG(5, "maxline = %d yplanesz = %d", maxline, yplanesz);
  451. DEBUG(3, "Frame state = %d", frame->scanstate);
  452. if(frame->scanstate == ScanState_Scanning) {
  453. int drop = 0;
  454. int curframe;
  455. int fdrops = 0;
  456. DEBUG(3, "Searching for marker, queue len = %d", RingQueue_GetLength(&uvd->dp));
  457. while(RingQueue_GetLength(&uvd->dp) >= 4) {
  458. if ((RING_QUEUE_PEEK(&uvd->dp, 0) == 0x00) &&
  459. (RING_QUEUE_PEEK(&uvd->dp, 1) == 0xff) &&
  460. (RING_QUEUE_PEEK(&uvd->dp, 2) == 0x00) &&
  461. (RING_QUEUE_PEEK(&uvd->dp, 3) < 0x80)) {
  462. curframe = RING_QUEUE_PEEK(&uvd->dp, 3);
  463. if(cam->lastframe >= 0) {
  464. fdrops = (0x80 + curframe - cam->lastframe) & 0x7F;
  465. fdrops--;
  466. if(fdrops) {
  467. info("Dropped %d frames (%d -> %d)", fdrops,
  468. cam->lastframe, curframe);
  469. }
  470. }
  471. cam->lastframe = curframe;
  472. frame->curline = 0;
  473. frame->scanstate = ScanState_Lines;
  474. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 4);
  475. break;
  476. }
  477. RING_QUEUE_DEQUEUE_BYTES(&uvd->dp, 1);
  478. drop++;
  479. }
  480. if(drop)
  481. DEBUG(2, "dropped %d bytes looking for new frame", drop);
  482. }
  483. if(frame->scanstate == ScanState_Scanning)
  484. return;
  485. /* Try to move data from queue into frame buffer
  486. * We get data in blocks of 384 bytes made up of:
  487. * 256 Y, 64 U, 64 V.
  488. * This needs to be written out as a Y plane, a U plane and a V plane.
  489. */
  490. while ( frame->curline < maxline && (RingQueue_GetLength(&uvd->dp) >= 384)) {
  491. /* Y */
  492. RingQueue_Dequeue(&uvd->dp, frame->data + (frame->curline * 256), 256);
  493. /* U */
  494. RingQueue_Dequeue(&uvd->dp, frame->data + yplanesz + (frame->curline * 64), 64);
  495. /* V */
  496. RingQueue_Dequeue(&uvd->dp, frame->data + (5 * yplanesz)/4 + (frame->curline * 64), 64);
  497. frame->seqRead_Length += 384;
  498. frame->curline++;
  499. }
  500. /* See if we filled the frame */
  501. if (frame->curline == maxline) {
  502. DEBUG(5, "got whole frame");
  503. frame->frameState = FrameState_Done_Hold;
  504. frame->curline = 0;
  505. uvd->curframe = -1;
  506. uvd->stats.frame_num++;
  507. }
  508. }
  509. static int konicawc_find_fps(int size, int fps)
  510. {
  511. int i;
  512. fps *= 3;
  513. DEBUG(1, "konica_find_fps: size = %d fps = %d", size, fps);
  514. if(fps <= spd_to_fps[size][0])
  515. return 0;
  516. if(fps >= spd_to_fps[size][MAX_SPEED])
  517. return MAX_SPEED;
  518. for(i = 0; i < MAX_SPEED; i++) {
  519. if((fps >= spd_to_fps[size][i]) && (fps <= spd_to_fps[size][i+1])) {
  520. DEBUG(2, "fps %d between %d and %d", fps, i, i+1);
  521. if( (fps - spd_to_fps[size][i]) < (spd_to_fps[size][i+1] - fps))
  522. return i;
  523. else
  524. return i+1;
  525. }
  526. }
  527. return MAX_SPEED+1;
  528. }
  529. static int konicawc_set_video_mode(struct uvd *uvd, struct video_window *vw)
  530. {
  531. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  532. int newspeed = cam->speed;
  533. int newsize;
  534. int x = vw->width;
  535. int y = vw->height;
  536. int fps = vw->flags;
  537. if(x > 0 && y > 0) {
  538. DEBUG(2, "trying to find size %d,%d", x, y);
  539. for(newsize = 0; newsize <= MAX_FRAME_SIZE; newsize++) {
  540. if((camera_sizes[newsize].width == x) && (camera_sizes[newsize].height == y))
  541. break;
  542. }
  543. } else {
  544. newsize = cam->size;
  545. }
  546. if(newsize > MAX_FRAME_SIZE) {
  547. DEBUG(1, "couldn't find size %d,%d", x, y);
  548. return -EINVAL;
  549. }
  550. if(fps > 0) {
  551. DEBUG(1, "trying to set fps to %d", fps);
  552. newspeed = konicawc_find_fps(newsize, fps);
  553. DEBUG(1, "find_fps returned %d (%d)", newspeed, spd_to_fps[newsize][newspeed]);
  554. }
  555. if(newspeed > MAX_SPEED)
  556. return -EINVAL;
  557. DEBUG(1, "setting size to %d speed to %d", newsize, newspeed);
  558. if((newsize == cam->size) && (newspeed == cam->speed)) {
  559. DEBUG(1, "Nothing to do");
  560. return 0;
  561. }
  562. DEBUG(0, "setting to %dx%d @ %d fps", camera_sizes[newsize].width,
  563. camera_sizes[newsize].height, spd_to_fps[newsize][newspeed]/3);
  564. konicawc_stop_data(uvd);
  565. uvd->ifaceAltActive = spd_to_iface[newspeed];
  566. DEBUG(1, "new interface = %d", uvd->ifaceAltActive);
  567. cam->speed = newspeed;
  568. if(cam->size != newsize) {
  569. cam->size = newsize;
  570. konicawc_set_camera_size(uvd);
  571. }
  572. /* Flush the input queue and clear any current frame in progress */
  573. RingQueue_Flush(&uvd->dp);
  574. cam->lastframe = -2;
  575. if(uvd->curframe != -1) {
  576. uvd->frame[uvd->curframe].curline = 0;
  577. uvd->frame[uvd->curframe].seqRead_Length = 0;
  578. uvd->frame[uvd->curframe].seqRead_Index = 0;
  579. }
  580. konicawc_start_data(uvd);
  581. return 0;
  582. }
  583. static int konicawc_calculate_fps(struct uvd *uvd)
  584. {
  585. struct konicawc *cam = uvd->user_data;
  586. return spd_to_fps[cam->size][cam->speed]/3;
  587. }
  588. static void konicawc_configure_video(struct uvd *uvd)
  589. {
  590. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  591. u8 buf[2];
  592. memset(&uvd->vpic, 0, sizeof(uvd->vpic));
  593. memset(&uvd->vpic_old, 0x55, sizeof(uvd->vpic_old));
  594. RESTRICT_TO_RANGE(brightness, 0, MAX_BRIGHTNESS);
  595. RESTRICT_TO_RANGE(contrast, 0, MAX_CONTRAST);
  596. RESTRICT_TO_RANGE(saturation, 0, MAX_SATURATION);
  597. RESTRICT_TO_RANGE(sharpness, 0, MAX_SHARPNESS);
  598. RESTRICT_TO_RANGE(whitebal, 0, MAX_WHITEBAL);
  599. cam->brightness = brightness / 11;
  600. cam->contrast = contrast / 11;
  601. cam->saturation = saturation / 11;
  602. cam->sharpness = sharpness / 11;
  603. cam->white_bal = whitebal / 11;
  604. uvd->vpic.colour = 108;
  605. uvd->vpic.hue = 108;
  606. uvd->vpic.brightness = brightness;
  607. uvd->vpic.contrast = contrast;
  608. uvd->vpic.whiteness = whitebal;
  609. uvd->vpic.depth = 6;
  610. uvd->vpic.palette = VIDEO_PALETTE_YUV420P;
  611. memset(&uvd->vcap, 0, sizeof(uvd->vcap));
  612. strcpy(uvd->vcap.name, "Konica Webcam");
  613. uvd->vcap.type = VID_TYPE_CAPTURE;
  614. uvd->vcap.channels = 1;
  615. uvd->vcap.audios = 0;
  616. uvd->vcap.minwidth = camera_sizes[SIZE_160X120].width;
  617. uvd->vcap.minheight = camera_sizes[SIZE_160X120].height;
  618. uvd->vcap.maxwidth = camera_sizes[SIZE_320X240].width;
  619. uvd->vcap.maxheight = camera_sizes[SIZE_320X240].height;
  620. memset(&uvd->vchan, 0, sizeof(uvd->vchan));
  621. uvd->vchan.flags = 0 ;
  622. uvd->vchan.tuners = 0;
  623. uvd->vchan.channel = 0;
  624. uvd->vchan.type = VIDEO_TYPE_CAMERA;
  625. strcpy(uvd->vchan.name, "Camera");
  626. /* Talk to device */
  627. DEBUG(1, "device init");
  628. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  629. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  630. if(!konicawc_get_misc(uvd, 0x3, 0, 0x10, buf, 2))
  631. DEBUG(2, "3,10 -> %2.2x %2.2x", buf[0], buf[1]);
  632. if(konicawc_set_misc(uvd, 0x2, 0, 0xd))
  633. DEBUG(2, "2,0,d failed");
  634. DEBUG(1, "setting initial values");
  635. }
  636. static int konicawc_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  637. {
  638. struct usb_device *dev = interface_to_usbdev(intf);
  639. struct uvd *uvd = NULL;
  640. int ix, i, nas;
  641. int actInterface=-1, inactInterface=-1, maxPS=0;
  642. unsigned char video_ep = 0;
  643. DEBUG(1, "konicawc_probe(%p)", intf);
  644. /* We don't handle multi-config cameras */
  645. if (dev->descriptor.bNumConfigurations != 1)
  646. return -ENODEV;
  647. info("Konica Webcam (rev. 0x%04x)", le16_to_cpu(dev->descriptor.bcdDevice));
  648. RESTRICT_TO_RANGE(speed, 0, MAX_SPEED);
  649. /* Validate found interface: must have one ISO endpoint */
  650. nas = intf->num_altsetting;
  651. if (nas != 8) {
  652. err("Incorrect number of alternate settings (%d) for this camera!", nas);
  653. return -ENODEV;
  654. }
  655. /* Validate all alternate settings */
  656. for (ix=0; ix < nas; ix++) {
  657. const struct usb_host_interface *interface;
  658. const struct usb_endpoint_descriptor *endpoint;
  659. interface = &intf->altsetting[ix];
  660. i = interface->desc.bAlternateSetting;
  661. if (interface->desc.bNumEndpoints != 2) {
  662. err("Interface %d. has %u. endpoints!",
  663. interface->desc.bInterfaceNumber,
  664. (unsigned)(interface->desc.bNumEndpoints));
  665. return -ENODEV;
  666. }
  667. endpoint = &interface->endpoint[1].desc;
  668. DEBUG(1, "found endpoint: addr: 0x%2.2x maxps = 0x%4.4x",
  669. endpoint->bEndpointAddress, le16_to_cpu(endpoint->wMaxPacketSize));
  670. if (video_ep == 0)
  671. video_ep = endpoint->bEndpointAddress;
  672. else if (video_ep != endpoint->bEndpointAddress) {
  673. err("Alternate settings have different endpoint addresses!");
  674. return -ENODEV;
  675. }
  676. if ((endpoint->bmAttributes & 0x03) != 0x01) {
  677. err("Interface %d. has non-ISO endpoint!",
  678. interface->desc.bInterfaceNumber);
  679. return -ENODEV;
  680. }
  681. if ((endpoint->bEndpointAddress & 0x80) == 0) {
  682. err("Interface %d. has ISO OUT endpoint!",
  683. interface->desc.bInterfaceNumber);
  684. return -ENODEV;
  685. }
  686. if (le16_to_cpu(endpoint->wMaxPacketSize) == 0) {
  687. if (inactInterface < 0)
  688. inactInterface = i;
  689. else {
  690. err("More than one inactive alt. setting!");
  691. return -ENODEV;
  692. }
  693. } else {
  694. if (i == spd_to_iface[speed]) {
  695. /* This one is the requested one */
  696. actInterface = i;
  697. }
  698. }
  699. if (le16_to_cpu(endpoint->wMaxPacketSize) > maxPS)
  700. maxPS = le16_to_cpu(endpoint->wMaxPacketSize);
  701. }
  702. if(actInterface == -1) {
  703. err("Cant find required endpoint");
  704. return -ENODEV;
  705. }
  706. DEBUG(1, "Selecting requested active setting=%d. maxPS=%d.", actInterface, maxPS);
  707. uvd = usbvideo_AllocateDevice(cams);
  708. if (uvd != NULL) {
  709. struct konicawc *cam = (struct konicawc *)(uvd->user_data);
  710. /* Here uvd is a fully allocated uvd object */
  711. for(i = 0; i < USBVIDEO_NUMSBUF; i++) {
  712. cam->sts_urb[i] = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  713. if(cam->sts_urb[i] == NULL) {
  714. while(i--) {
  715. usb_free_urb(cam->sts_urb[i]);
  716. }
  717. err("can't allocate urbs");
  718. return -ENOMEM;
  719. }
  720. }
  721. cam->speed = speed;
  722. RESTRICT_TO_RANGE(size, SIZE_160X120, SIZE_320X240);
  723. cam->width = camera_sizes[size].width;
  724. cam->height = camera_sizes[size].height;
  725. cam->size = size;
  726. uvd->flags = 0;
  727. uvd->debug = debug;
  728. uvd->dev = dev;
  729. uvd->iface = intf->altsetting->desc.bInterfaceNumber;
  730. uvd->ifaceAltInactive = inactInterface;
  731. uvd->ifaceAltActive = actInterface;
  732. uvd->video_endp = video_ep;
  733. uvd->iso_packet_len = maxPS;
  734. uvd->paletteBits = 1L << VIDEO_PALETTE_YUV420P;
  735. uvd->defaultPalette = VIDEO_PALETTE_YUV420P;
  736. uvd->canvas = VIDEOSIZE(320, 240);
  737. uvd->videosize = VIDEOSIZE(cam->width, cam->height);
  738. /* Initialize konicawc specific data */
  739. konicawc_configure_video(uvd);
  740. i = usbvideo_RegisterVideoDevice(uvd);
  741. uvd->max_frame_size = (320 * 240 * 3)/2;
  742. if (i != 0) {
  743. err("usbvideo_RegisterVideoDevice() failed.");
  744. uvd = NULL;
  745. }
  746. konicawc_register_input(cam, dev);
  747. }
  748. if (uvd) {
  749. usb_set_intfdata (intf, uvd);
  750. return 0;
  751. }
  752. return -EIO;
  753. }
  754. static void konicawc_free_uvd(struct uvd *uvd)
  755. {
  756. int i;
  757. struct konicawc *cam = (struct konicawc *)uvd->user_data;
  758. konicawc_unregister_input(cam);
  759. for (i = 0; i < USBVIDEO_NUMSBUF; i++) {
  760. usb_free_urb(cam->sts_urb[i]);
  761. cam->sts_urb[i] = NULL;
  762. }
  763. }
  764. static struct usb_device_id id_table[] = {
  765. { USB_DEVICE(0x04c8, 0x0720) }, /* Intel YC 76 */
  766. { } /* Terminating entry */
  767. };
  768. static int __init konicawc_init(void)
  769. {
  770. struct usbvideo_cb cbTbl;
  771. info(DRIVER_DESC " " DRIVER_VERSION);
  772. memset(&cbTbl, 0, sizeof(cbTbl));
  773. cbTbl.probe = konicawc_probe;
  774. cbTbl.setupOnOpen = konicawc_setup_on_open;
  775. cbTbl.processData = konicawc_process_isoc;
  776. cbTbl.getFPS = konicawc_calculate_fps;
  777. cbTbl.setVideoMode = konicawc_set_video_mode;
  778. cbTbl.startDataPump = konicawc_start_data;
  779. cbTbl.stopDataPump = konicawc_stop_data;
  780. cbTbl.adjustPicture = konicawc_adjust_picture;
  781. cbTbl.userFree = konicawc_free_uvd;
  782. return usbvideo_register(
  783. &cams,
  784. MAX_CAMERAS,
  785. sizeof(struct konicawc),
  786. "konicawc",
  787. &cbTbl,
  788. THIS_MODULE,
  789. id_table);
  790. }
  791. static void __exit konicawc_cleanup(void)
  792. {
  793. usbvideo_Deregister(&cams);
  794. }
  795. MODULE_DEVICE_TABLE(usb, id_table);
  796. MODULE_LICENSE("GPL");
  797. MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
  798. MODULE_DESCRIPTION(DRIVER_DESC);
  799. module_param(speed, int, 0);
  800. MODULE_PARM_DESC(speed, "Initial speed: 0 (slowest) - 6 (fastest)");
  801. module_param(size, int, 0);
  802. MODULE_PARM_DESC(size, "Initial Size 0: 160x120 1: 160x136 2: 176x144 3: 320x240");
  803. module_param(brightness, int, 0);
  804. MODULE_PARM_DESC(brightness, "Initial brightness 0 - 108");
  805. module_param(contrast, int, 0);
  806. MODULE_PARM_DESC(contrast, "Initial contrast 0 - 108");
  807. module_param(saturation, int, 0);
  808. MODULE_PARM_DESC(saturation, "Initial saturation 0 - 108");
  809. module_param(sharpness, int, 0);
  810. MODULE_PARM_DESC(sharpness, "Initial brightness 0 - 108");
  811. module_param(whitebal, int, 0);
  812. MODULE_PARM_DESC(whitebal, "Initial white balance 0 - 363");
  813. #ifdef CONFIG_USB_DEBUG
  814. module_param(debug, int, S_IRUGO | S_IWUSR);
  815. MODULE_PARM_DESC(debug, "Debug level: 0-9 (default=0)");
  816. #endif
  817. module_init(konicawc_init);
  818. module_exit(konicawc_cleanup);