blob: 636627b57dc933ff447c1f2485ae031e173bdb74 [file] [log] [blame]
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -03001/*
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
Joe Perches133a9fe2011-08-21 19:56:57 -030021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030023#define MODULE_NAME "benq"
24
25#include "gspca.h"
26
27MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
28MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver");
29MODULE_LICENSE("GPL");
30
31/* specific webcam descriptor */
32struct sd {
33 struct gspca_dev gspca_dev; /* !! must be the first item */
34};
35
36/* V4L2 controls supported by the driver */
Marton Nemeth7e64dc42009-12-30 09:12:41 -030037static const struct ctrl sd_ctrls[] = {
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030038};
39
40static const struct v4l2_pix_format vga_mode[] = {
41 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
42 .bytesperline = 320,
43 .sizeimage = 320 * 240 * 3 / 8 + 590,
44 .colorspace = V4L2_COLORSPACE_JPEG},
45};
46
47static void sd_isoc_irq(struct urb *urb);
48
49/* -- write a register -- */
50static void reg_w(struct gspca_dev *gspca_dev,
51 u16 value, u16 index)
52{
53 struct usb_device *dev = gspca_dev->dev;
54 int ret;
55
56 if (gspca_dev->usb_err < 0)
57 return;
58 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
59 0x02,
60 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
61 value,
62 index,
63 NULL,
64 0,
65 500);
66 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -030067 pr_err("reg_w err %d\n", ret);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030068 gspca_dev->usb_err = ret;
69 }
70}
71
72/* this function is called at probe time */
73static int sd_config(struct gspca_dev *gspca_dev,
74 const struct usb_device_id *id)
75{
76 gspca_dev->cam.cam_mode = vga_mode;
77 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
78 gspca_dev->cam.no_urb_create = 1;
79 gspca_dev->cam.reverse_alts = 1;
80 return 0;
81}
82
83/* this function is called at probe and resume time */
84static int sd_init(struct gspca_dev *gspca_dev)
85{
86 return 0;
87}
88
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -030089/* -- start the camera -- */
90static int sd_start(struct gspca_dev *gspca_dev)
91{
92 struct urb *urb;
93 int i, n;
94
95 /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */
96#if MAX_NURBS < 4
97#error "Not enough URBs in the gspca table"
98#endif
99#define SD_PKT_SZ 64
100#define SD_NPKT 32
101 for (n = 0; n < 4; n++) {
102 urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL);
103 if (!urb) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300104 pr_err("usb_alloc_urb failed\n");
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300105 return -ENOMEM;
106 }
107 gspca_dev->urb[n] = urb;
Daniel Mack997ea582010-04-12 13:17:25 +0200108 urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev,
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300109 SD_PKT_SZ * SD_NPKT,
110 GFP_KERNEL,
111 &urb->transfer_dma);
112
113 if (urb->transfer_buffer == NULL) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300114 pr_err("usb_alloc_coherent failed\n");
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300115 return -ENOMEM;
116 }
117 urb->dev = gspca_dev->dev;
118 urb->context = gspca_dev;
119 urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT;
120 urb->pipe = usb_rcvisocpipe(gspca_dev->dev,
121 n & 1 ? 0x82 : 0x83);
122 urb->transfer_flags = URB_ISO_ASAP
123 | URB_NO_TRANSFER_DMA_MAP;
124 urb->interval = 1;
125 urb->complete = sd_isoc_irq;
126 urb->number_of_packets = SD_NPKT;
127 for (i = 0; i < SD_NPKT; i++) {
128 urb->iso_frame_desc[i].length = SD_PKT_SZ;
129 urb->iso_frame_desc[i].offset = SD_PKT_SZ * i;
130 }
131 }
132
133 return gspca_dev->usb_err;
134}
135
136static void sd_stopN(struct gspca_dev *gspca_dev)
137{
138 reg_w(gspca_dev, 0x003c, 0x0003);
139 reg_w(gspca_dev, 0x003c, 0x0004);
140 reg_w(gspca_dev, 0x003c, 0x0005);
141 reg_w(gspca_dev, 0x003c, 0x0006);
142 reg_w(gspca_dev, 0x003c, 0x0007);
Jean-François Moine780e3122010-10-19 04:29:10 -0300143 usb_set_interface(gspca_dev->dev, gspca_dev->iface,
144 gspca_dev->nbalt - 1);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300145}
146
147static void sd_pkt_scan(struct gspca_dev *gspca_dev,
148 u8 *data, /* isoc packet */
149 int len) /* iso packet length */
150{
151 /* unused */
152}
153
154/* reception of an URB */
155static void sd_isoc_irq(struct urb *urb)
156{
157 struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context;
158 struct urb *urb0;
159 u8 *data;
160 int i, st;
161
162 PDEBUG(D_PACK, "sd isoc irq");
163 if (!gspca_dev->streaming)
164 return;
165 if (urb->status != 0) {
166 if (urb->status == -ESHUTDOWN)
167 return; /* disconnection */
168#ifdef CONFIG_PM
169 if (gspca_dev->frozen)
170 return;
171#endif
Joe Perches133a9fe2011-08-21 19:56:57 -0300172 pr_err("urb status: %d\n", urb->status);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300173 return;
174 }
175
176 /* if this is a control URN (ep 0x83), wait */
177 if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2])
178 return;
179
180 /* scan both received URBs */
181 if (urb == gspca_dev->urb[1])
182 urb0 = gspca_dev->urb[0];
183 else
184 urb0 = gspca_dev->urb[2];
185 for (i = 0; i < urb->number_of_packets; i++) {
186
187 /* check the packet status and length */
188 if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ
189 || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) {
190 PDEBUG(D_ERR, "ISOC bad lengths %d / %d",
191 urb0->iso_frame_desc[i].actual_length,
192 urb->iso_frame_desc[i].actual_length);
193 gspca_dev->last_packet_type = DISCARD_PACKET;
194 continue;
195 }
196 st = urb0->iso_frame_desc[i].status;
197 if (st == 0)
198 st = urb->iso_frame_desc[i].status;
199 if (st) {
Joe Perches133a9fe2011-08-21 19:56:57 -0300200 pr_err("ISOC data error: [%d] status=%d\n",
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300201 i, st);
202 gspca_dev->last_packet_type = DISCARD_PACKET;
203 continue;
204 }
205
206 /*
207 * The images are received in URBs of different endpoints
208 * (0x83 and 0x82).
209 * Image pieces in URBs of ep 0x83 are continuated in URBs of
210 * ep 0x82 of the same index.
211 * The packets in the URBs of endpoint 0x83 start with:
212 * - 80 ba/bb 00 00 = start of image followed by 'ff d8'
213 * - 04 ba/bb oo oo = image piece
214 * where 'oo oo' is the image offset
215 (not cheked)
216 * - (other -> bad frame)
217 * The images are JPEG encoded with full header and
218 * normal ff escape.
219 * The end of image ('ff d9') may occur in any URB.
220 * (not cheked)
221 */
222 data = (u8 *) urb0->transfer_buffer
223 + urb0->iso_frame_desc[i].offset;
224 if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) {
225
226 /* new image */
227 gspca_frame_add(gspca_dev, LAST_PACKET,
228 NULL, 0);
229 gspca_frame_add(gspca_dev, FIRST_PACKET,
230 data + 4, SD_PKT_SZ - 4);
231 } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) {
232 gspca_frame_add(gspca_dev, INTER_PACKET,
233 data + 4, SD_PKT_SZ - 4);
234 } else {
235 gspca_dev->last_packet_type = DISCARD_PACKET;
236 continue;
237 }
238 data = (u8 *) urb->transfer_buffer
239 + urb->iso_frame_desc[i].offset;
240 gspca_frame_add(gspca_dev, INTER_PACKET,
241 data, SD_PKT_SZ);
242 }
243
244 /* resubmit the URBs */
245 st = usb_submit_urb(urb0, GFP_ATOMIC);
246 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300247 pr_err("usb_submit_urb(0) ret %d\n", st);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300248 st = usb_submit_urb(urb, GFP_ATOMIC);
249 if (st < 0)
Joe Perches133a9fe2011-08-21 19:56:57 -0300250 pr_err("usb_submit_urb() ret %d\n", st);
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300251}
252
253/* sub-driver description */
254static const struct sd_desc sd_desc = {
255 .name = MODULE_NAME,
256 .ctrls = sd_ctrls,
257 .nctrls = ARRAY_SIZE(sd_ctrls),
258 .config = sd_config,
259 .init = sd_init,
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300260 .start = sd_start,
261 .stopN = sd_stopN,
262 .pkt_scan = sd_pkt_scan,
263};
264
265/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -0300266static const struct usb_device_id device_table[] = {
Jean-Francois Moine0a71d9c2009-12-20 09:09:22 -0300267 {USB_DEVICE(0x04a5, 0x3035)},
268 {}
269};
270MODULE_DEVICE_TABLE(usb, device_table);
271
272/* -- device connect -- */
273static int sd_probe(struct usb_interface *intf,
274 const struct usb_device_id *id)
275{
276 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
277 THIS_MODULE);
278}
279
280static struct usb_driver sd_driver = {
281 .name = MODULE_NAME,
282 .id_table = device_table,
283 .probe = sd_probe,
284 .disconnect = gspca_disconnect,
285#ifdef CONFIG_PM
286 .suspend = gspca_suspend,
287 .resume = gspca_resume,
288#endif
289};
290
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800291module_usb_driver(sd_driver);