benq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Benq DC E300 subdriver
  3. *
  4. * Copyright (C) 2009 Jean-Francois Moine (http://moinejf.free.fr)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #define MODULE_NAME "benq"
  18. #include "gspca.h"
  19. MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
  20. MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
  21. MODULE_LICENSE("GPL");
  22. /* specific webcam descriptor */
  23. struct sd {
  24. struct gspca_dev gspca_dev; /* !! must be the first item */
  25. };
  26. static const struct v4l2_pix_format vga_mode[] = {
  27. {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  28. .bytesperline = 320,
  29. .sizeimage = 320 * 240 * 3 / 8 + 590,
  30. .colorspace = V4L2_COLORSPACE_JPEG},
  31. };
  32. static void sd_isoc_irq(struct urb *urb);
  33. /* -- write a register -- */
  34. static void reg_w(struct gspca_dev *gspca_dev,
  35. u16 value, u16 index)
  36. {
  37. struct usb_device *dev = gspca_dev->dev;
  38. int ret;
  39. if (gspca_dev->usb_err < 0)
  40. return;
  41. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  42. 0x02,
  43. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  44. value,
  45. index,
  46. NULL,
  47. 0,
  48. 500);
  49. if (ret < 0) {
  50. pr_err("reg_w err %d\n", ret);
  51. gspca_dev->usb_err = ret;
  52. }
  53. }
  54. /* this function is called at probe time */
  55. static int sd_config(struct gspca_dev *gspca_dev,
  56. const struct usb_device_id *id)
  57. {
  58. gspca_dev->cam.cam_mode = vga_mode;
  59. gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
  60. gspca_dev->cam.no_urb_create = 1;
  61. return 0;
  62. }
  63. /* this function is called at probe and resume time */
  64. static int sd_init(struct gspca_dev *gspca_dev)
  65. {
  66. return 0;
  67. }
  68. /* -- start the camera -- */
  69. static int sd_start(struct gspca_dev *gspca_dev)
  70. {
  71. struct urb *urb;
  72. int i, n;
  73. /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
  74. #if MAX_NURBS < 4
  75. #error "Not enough URBs in the gspca table"
  76. #endif
  77. #define SD_PKT_SZ 64
  78. #define SD_NPKT 32
  79. for (n = 0; n < 4; n++) {
  80. urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
  81. if (!urb)
  82. return -ENOMEM;
  83. gspca_dev->urb[n] = urb;
  84. urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
  85. SD_PKT_SZ * SD_NPKT,
  86. GFP_KERNEL,
  87. &urb->transfer_dma);
  88. if (urb->transfer_buffer == NULL) {
  89. pr_err("usb_alloc_coherent failed\n");
  90. return -ENOMEM;
  91. }
  92. urb->dev = gspca_dev->dev;
  93. urb->context = gspca_dev;
  94. urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
  95. urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
  96. n & 1 ? 0x82 : 0x83);
  97. urb->transfer_flags = URB_ISO_ASAP
  98. | URB_NO_TRANSFER_DMA_MAP;
  99. urb->interval = 1;
  100. urb->complete = sd_isoc_irq;
  101. urb->number_of_packets = SD_NPKT;
  102. for (i = 0; i < SD_NPKT; i++) {
  103. urb->iso_frame_desc[i].length = SD_PKT_SZ;
  104. urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
  105. }
  106. }
  107. return gspca_dev->usb_err;
  108. }
  109. static void sd_stopN(struct gspca_dev *gspca_dev)
  110. {
  111. struct usb_interface *intf;
  112. reg_w(gspca_dev, 0x003c, 0x0003);
  113. reg_w(gspca_dev, 0x003c, 0x0004);
  114. reg_w(gspca_dev, 0x003c, 0x0005);
  115. reg_w(gspca_dev, 0x003c, 0x0006);
  116. reg_w(gspca_dev, 0x003c, 0x0007);
  117. intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface);
  118. usb_set_interface(gspca_dev->dev, gspca_dev->iface,
  119. intf->num_altsetting - 1);
  120. }
  121. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  122. u8 *data, /* isoc packet */
  123. int len) /* iso packet length */
  124. {
  125. /* unused */
  126. }
  127. /* reception of an URB */
  128. static void sd_isoc_irq(struct urb *urb)
  129. {
  130. struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
  131. struct urb *urb0;
  132. u8 *data;
  133. int i, st;
  134. gspca_dbg(gspca_dev, D_PACK, "sd isoc irq\n");
  135. if (!gspca_dev->streaming)
  136. return;
  137. if (urb->status != 0) {
  138. if (urb->status == -ESHUTDOWN)
  139. return; /* disconnection */
  140. #ifdef CONFIG_PM
  141. if (gspca_dev->frozen)
  142. return;
  143. #endif
  144. pr_err("urb status: %d\n", urb->status);
  145. return;
  146. }
  147. /* if this is a control URN (ep 0x83), wait */
  148. if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
  149. return;
  150. /* scan both received URBs */
  151. if (urb == gspca_dev->urb[1])
  152. urb0 = gspca_dev->urb[0];
  153. else
  154. urb0 = gspca_dev->urb[2];
  155. for (i = 0; i < urb->number_of_packets; i++) {
  156. /* check the packet status and length */
  157. if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
  158. || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
  159. gspca_err(gspca_dev, "ISOC bad lengths %d / %d\n",
  160. urb0->iso_frame_desc[i].actual_length,
  161. urb->iso_frame_desc[i].actual_length);
  162. gspca_dev->last_packet_type = DISCARD_PACKET;
  163. continue;
  164. }
  165. st = urb0->iso_frame_desc[i].status;
  166. if (st == 0)
  167. st = urb->iso_frame_desc[i].status;
  168. if (st) {
  169. pr_err("ISOC data error: [%d] status=%d\n",
  170. i, st);
  171. gspca_dev->last_packet_type = DISCARD_PACKET;
  172. continue;
  173. }
  174. /*
  175. * The images are received in URBs of different endpoints
  176. * (0x83 and 0x82).
  177. * Image pieces in URBs of ep 0x83 are continuated in URBs of
  178. * ep 0x82 of the same index.
  179. * The packets in the URBs of endpoint 0x83 start with:
  180. * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
  181. * - 04 ba/bb oo oo = image piece
  182. * where 'oo oo' is the image offset
  183. (not cheked)
  184. * - (other -> bad frame)
  185. * The images are JPEG encoded with full header and
  186. * normal ff escape.
  187. * The end of image ('ff d9') may occur in any URB.
  188. * (not cheked)
  189. */
  190. data = (u8 *) urb0->transfer_buffer
  191. + urb0->iso_frame_desc[i].offset;
  192. if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
  193. /* new image */
  194. gspca_frame_add(gspca_dev, LAST_PACKET,
  195. NULL, 0);
  196. gspca_frame_add(gspca_dev, FIRST_PACKET,
  197. data + 4, SD_PKT_SZ - 4);
  198. } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
  199. gspca_frame_add(gspca_dev, INTER_PACKET,
  200. data + 4, SD_PKT_SZ - 4);
  201. } else {
  202. gspca_dev->last_packet_type = DISCARD_PACKET;
  203. continue;
  204. }
  205. data = (u8 *) urb->transfer_buffer
  206. + urb->iso_frame_desc[i].offset;
  207. gspca_frame_add(gspca_dev, INTER_PACKET,
  208. data, SD_PKT_SZ);
  209. }
  210. /* resubmit the URBs */
  211. st = usb_submit_urb(urb0, GFP_ATOMIC);
  212. if (st < 0)
  213. pr_err("usb_submit_urb(0) ret %d\n", st);
  214. st = usb_submit_urb(urb, GFP_ATOMIC);
  215. if (st < 0)
  216. pr_err("usb_submit_urb() ret %d\n", st);
  217. }
  218. /* sub-driver description */
  219. static const struct sd_desc sd_desc = {
  220. .name = MODULE_NAME,
  221. .config = sd_config,
  222. .init = sd_init,
  223. .start = sd_start,
  224. .stopN = sd_stopN,
  225. .pkt_scan = sd_pkt_scan,
  226. };
  227. /* -- module initialisation -- */
  228. static const struct usb_device_id device_table[] = {
  229. {USB_DEVICE(0x04a5, 0x3035)},
  230. {}
  231. };
  232. MODULE_DEVICE_TABLE(usb, device_table);
  233. /* -- device connect -- */
  234. static int sd_probe(struct usb_interface *intf,
  235. const struct usb_device_id *id)
  236. {
  237. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  238. THIS_MODULE);
  239. }
  240. static struct usb_driver sd_driver = {
  241. .name = MODULE_NAME,
  242. .id_table = device_table,
  243. .probe = sd_probe,
  244. .disconnect = gspca_disconnect,
  245. #ifdef CONFIG_PM
  246. .suspend = gspca_suspend,
  247. .resume = gspca_resume,
  248. .reset_resume = gspca_resume,
  249. #endif
  250. };
  251. module_usb_driver(sd_driver);