benq.c 7.4 KB

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