Commit | Line | Data |
---|---|---|
0a71d9ce JFM |
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 | ||
133a9fe9 JP |
21 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
22 | ||
0a71d9ce JFM |
23 | #define MODULE_NAME "benq" |
24 | ||
25 | #include "gspca.h" | |
26 | ||
27 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); | |
28 | MODULE_DESCRIPTION("Benq DC E300 USB Camera Driver"); | |
29 | MODULE_LICENSE("GPL"); | |
30 | ||
31 | /* specific webcam descriptor */ | |
32 | struct sd { | |
33 | struct gspca_dev gspca_dev; /* !! must be the first item */ | |
34 | }; | |
35 | ||
36 | /* V4L2 controls supported by the driver */ | |
7e64dc4c | 37 | static const struct ctrl sd_ctrls[] = { |
0a71d9ce JFM |
38 | }; |
39 | ||
40 | static 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 | ||
47 | static void sd_isoc_irq(struct urb *urb); | |
48 | ||
49 | /* -- write a register -- */ | |
50 | static 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) { | |
133a9fe9 | 67 | pr_err("reg_w err %d\n", ret); |
0a71d9ce JFM |
68 | gspca_dev->usb_err = ret; |
69 | } | |
70 | } | |
71 | ||
72 | /* this function is called at probe time */ | |
73 | static 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 */ | |
84 | static int sd_init(struct gspca_dev *gspca_dev) | |
85 | { | |
86 | return 0; | |
87 | } | |
88 | ||
89 | static int sd_isoc_init(struct gspca_dev *gspca_dev) | |
90 | { | |
91 | int ret; | |
92 | ||
93 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, | |
94 | gspca_dev->nbalt - 1); | |
95 | if (ret < 0) { | |
133a9fe9 | 96 | pr_err("usb_set_interface failed\n"); |
0a71d9ce JFM |
97 | return ret; |
98 | } | |
99 | /* reg_w(gspca_dev, 0x0003, 0x0002); */ | |
100 | return 0; | |
101 | } | |
102 | ||
103 | /* -- start the camera -- */ | |
104 | static int sd_start(struct gspca_dev *gspca_dev) | |
105 | { | |
106 | struct urb *urb; | |
107 | int i, n; | |
108 | ||
109 | /* create 4 URBs - 2 on endpoint 0x83 and 2 on 0x082 */ | |
110 | #if MAX_NURBS < 4 | |
111 | #error "Not enough URBs in the gspca table" | |
112 | #endif | |
113 | #define SD_PKT_SZ 64 | |
114 | #define SD_NPKT 32 | |
115 | for (n = 0; n < 4; n++) { | |
116 | urb = usb_alloc_urb(SD_NPKT, GFP_KERNEL); | |
117 | if (!urb) { | |
133a9fe9 | 118 | pr_err("usb_alloc_urb failed\n"); |
0a71d9ce JFM |
119 | return -ENOMEM; |
120 | } | |
121 | gspca_dev->urb[n] = urb; | |
997ea58e | 122 | urb->transfer_buffer = usb_alloc_coherent(gspca_dev->dev, |
0a71d9ce JFM |
123 | SD_PKT_SZ * SD_NPKT, |
124 | GFP_KERNEL, | |
125 | &urb->transfer_dma); | |
126 | ||
127 | if (urb->transfer_buffer == NULL) { | |
133a9fe9 | 128 | pr_err("usb_alloc_coherent failed\n"); |
0a71d9ce JFM |
129 | return -ENOMEM; |
130 | } | |
131 | urb->dev = gspca_dev->dev; | |
132 | urb->context = gspca_dev; | |
133 | urb->transfer_buffer_length = SD_PKT_SZ * SD_NPKT; | |
134 | urb->pipe = usb_rcvisocpipe(gspca_dev->dev, | |
135 | n & 1 ? 0x82 : 0x83); | |
136 | urb->transfer_flags = URB_ISO_ASAP | |
137 | | URB_NO_TRANSFER_DMA_MAP; | |
138 | urb->interval = 1; | |
139 | urb->complete = sd_isoc_irq; | |
140 | urb->number_of_packets = SD_NPKT; | |
141 | for (i = 0; i < SD_NPKT; i++) { | |
142 | urb->iso_frame_desc[i].length = SD_PKT_SZ; | |
143 | urb->iso_frame_desc[i].offset = SD_PKT_SZ * i; | |
144 | } | |
145 | } | |
146 | ||
147 | return gspca_dev->usb_err; | |
148 | } | |
149 | ||
150 | static void sd_stopN(struct gspca_dev *gspca_dev) | |
151 | { | |
152 | reg_w(gspca_dev, 0x003c, 0x0003); | |
153 | reg_w(gspca_dev, 0x003c, 0x0004); | |
154 | reg_w(gspca_dev, 0x003c, 0x0005); | |
155 | reg_w(gspca_dev, 0x003c, 0x0006); | |
156 | reg_w(gspca_dev, 0x003c, 0x0007); | |
780e3121 JFM |
157 | usb_set_interface(gspca_dev->dev, gspca_dev->iface, |
158 | gspca_dev->nbalt - 1); | |
0a71d9ce JFM |
159 | } |
160 | ||
161 | static void sd_pkt_scan(struct gspca_dev *gspca_dev, | |
162 | u8 *data, /* isoc packet */ | |
163 | int len) /* iso packet length */ | |
164 | { | |
165 | /* unused */ | |
166 | } | |
167 | ||
168 | /* reception of an URB */ | |
169 | static void sd_isoc_irq(struct urb *urb) | |
170 | { | |
171 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; | |
172 | struct urb *urb0; | |
173 | u8 *data; | |
174 | int i, st; | |
175 | ||
176 | PDEBUG(D_PACK, "sd isoc irq"); | |
177 | if (!gspca_dev->streaming) | |
178 | return; | |
179 | if (urb->status != 0) { | |
180 | if (urb->status == -ESHUTDOWN) | |
181 | return; /* disconnection */ | |
182 | #ifdef CONFIG_PM | |
183 | if (gspca_dev->frozen) | |
184 | return; | |
185 | #endif | |
133a9fe9 | 186 | pr_err("urb status: %d\n", urb->status); |
0a71d9ce JFM |
187 | return; |
188 | } | |
189 | ||
190 | /* if this is a control URN (ep 0x83), wait */ | |
191 | if (urb == gspca_dev->urb[0] || urb == gspca_dev->urb[2]) | |
192 | return; | |
193 | ||
194 | /* scan both received URBs */ | |
195 | if (urb == gspca_dev->urb[1]) | |
196 | urb0 = gspca_dev->urb[0]; | |
197 | else | |
198 | urb0 = gspca_dev->urb[2]; | |
199 | for (i = 0; i < urb->number_of_packets; i++) { | |
200 | ||
201 | /* check the packet status and length */ | |
202 | if (urb0->iso_frame_desc[i].actual_length != SD_PKT_SZ | |
203 | || urb->iso_frame_desc[i].actual_length != SD_PKT_SZ) { | |
204 | PDEBUG(D_ERR, "ISOC bad lengths %d / %d", | |
205 | urb0->iso_frame_desc[i].actual_length, | |
206 | urb->iso_frame_desc[i].actual_length); | |
207 | gspca_dev->last_packet_type = DISCARD_PACKET; | |
208 | continue; | |
209 | } | |
210 | st = urb0->iso_frame_desc[i].status; | |
211 | if (st == 0) | |
212 | st = urb->iso_frame_desc[i].status; | |
213 | if (st) { | |
133a9fe9 | 214 | pr_err("ISOC data error: [%d] status=%d\n", |
0a71d9ce JFM |
215 | i, st); |
216 | gspca_dev->last_packet_type = DISCARD_PACKET; | |
217 | continue; | |
218 | } | |
219 | ||
220 | /* | |
221 | * The images are received in URBs of different endpoints | |
222 | * (0x83 and 0x82). | |
223 | * Image pieces in URBs of ep 0x83 are continuated in URBs of | |
224 | * ep 0x82 of the same index. | |
225 | * The packets in the URBs of endpoint 0x83 start with: | |
226 | * - 80 ba/bb 00 00 = start of image followed by 'ff d8' | |
227 | * - 04 ba/bb oo oo = image piece | |
228 | * where 'oo oo' is the image offset | |
229 | (not cheked) | |
230 | * - (other -> bad frame) | |
231 | * The images are JPEG encoded with full header and | |
232 | * normal ff escape. | |
233 | * The end of image ('ff d9') may occur in any URB. | |
234 | * (not cheked) | |
235 | */ | |
236 | data = (u8 *) urb0->transfer_buffer | |
237 | + urb0->iso_frame_desc[i].offset; | |
238 | if (data[0] == 0x80 && (data[1] & 0xfe) == 0xba) { | |
239 | ||
240 | /* new image */ | |
241 | gspca_frame_add(gspca_dev, LAST_PACKET, | |
242 | NULL, 0); | |
243 | gspca_frame_add(gspca_dev, FIRST_PACKET, | |
244 | data + 4, SD_PKT_SZ - 4); | |
245 | } else if (data[0] == 0x04 && (data[1] & 0xfe) == 0xba) { | |
246 | gspca_frame_add(gspca_dev, INTER_PACKET, | |
247 | data + 4, SD_PKT_SZ - 4); | |
248 | } else { | |
249 | gspca_dev->last_packet_type = DISCARD_PACKET; | |
250 | continue; | |
251 | } | |
252 | data = (u8 *) urb->transfer_buffer | |
253 | + urb->iso_frame_desc[i].offset; | |
254 | gspca_frame_add(gspca_dev, INTER_PACKET, | |
255 | data, SD_PKT_SZ); | |
256 | } | |
257 | ||
258 | /* resubmit the URBs */ | |
259 | st = usb_submit_urb(urb0, GFP_ATOMIC); | |
260 | if (st < 0) | |
133a9fe9 | 261 | pr_err("usb_submit_urb(0) ret %d\n", st); |
0a71d9ce JFM |
262 | st = usb_submit_urb(urb, GFP_ATOMIC); |
263 | if (st < 0) | |
133a9fe9 | 264 | pr_err("usb_submit_urb() ret %d\n", st); |
0a71d9ce JFM |
265 | } |
266 | ||
267 | /* sub-driver description */ | |
268 | static const struct sd_desc sd_desc = { | |
269 | .name = MODULE_NAME, | |
270 | .ctrls = sd_ctrls, | |
271 | .nctrls = ARRAY_SIZE(sd_ctrls), | |
272 | .config = sd_config, | |
273 | .init = sd_init, | |
274 | .isoc_init = sd_isoc_init, | |
275 | .start = sd_start, | |
276 | .stopN = sd_stopN, | |
277 | .pkt_scan = sd_pkt_scan, | |
278 | }; | |
279 | ||
280 | /* -- module initialisation -- */ | |
95c967c1 | 281 | static const struct usb_device_id device_table[] = { |
0a71d9ce JFM |
282 | {USB_DEVICE(0x04a5, 0x3035)}, |
283 | {} | |
284 | }; | |
285 | MODULE_DEVICE_TABLE(usb, device_table); | |
286 | ||
287 | /* -- device connect -- */ | |
288 | static int sd_probe(struct usb_interface *intf, | |
289 | const struct usb_device_id *id) | |
290 | { | |
291 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), | |
292 | THIS_MODULE); | |
293 | } | |
294 | ||
295 | static struct usb_driver sd_driver = { | |
296 | .name = MODULE_NAME, | |
297 | .id_table = device_table, | |
298 | .probe = sd_probe, | |
299 | .disconnect = gspca_disconnect, | |
300 | #ifdef CONFIG_PM | |
301 | .suspend = gspca_suspend, | |
302 | .resume = gspca_resume, | |
303 | #endif | |
304 | }; | |
305 | ||
306 | /* -- module insert / remove -- */ | |
307 | static int __init sd_mod_init(void) | |
308 | { | |
8129888a | 309 | return usb_register(&sd_driver); |
0a71d9ce JFM |
310 | } |
311 | static void __exit sd_mod_exit(void) | |
312 | { | |
313 | usb_deregister(&sd_driver); | |
0a71d9ce JFM |
314 | } |
315 | ||
316 | module_init(sd_mod_init); | |
317 | module_exit(sd_mod_exit); |