Import 2.3.99pre2-1
[davej-history.git] / drivers / usb / dc2xx.c
blob9dff6bda14f8a8b1e1309567a06fd734c2e058e8
1 /*
2 * Copyright (C) 1999-2000 by David Brownell <david-b@pacbell.net>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * USB driver for Kodak DC-2XX series digital still cameras
23 * The protocol here is the same as the one going over a serial line, but
24 * it uses USB for speed. Set up /dev/kodak, get gphoto (www.gphoto.org),
25 * and have fun!
27 * This should also work for a number of other digital (non-Kodak) cameras,
28 * by adding the vendor and product IDs to the table below. They'll need
29 * to be the sort using USB just as a fast bulk data channel.
33 * HISTORY
35 * 26 August, 1999 -- first release (0.1), works with my DC-240.
36 * The DC-280 (2Mpixel) should also work, but isn't tested.
37 * If you use gphoto, make sure you have the USB updates.
38 * Lives in a 2.3.14 or so Linux kernel, in drivers/usb.
39 * 31 August, 1999 -- minor update to recognize DC-260 and handle
40 * its endpoints being in a different order. Note that as
41 * of gPhoto 0.36pre, the USB updates are integrated.
42 * 12 Oct, 1999 -- handle DC-280 interface class (0xff not 0x0);
43 * added timeouts to bulk_msg calls. Minor updates, docs.
44 * 03 Nov, 1999 -- update for 2.3.25 kernel API changes.
45 * 08 Jan, 2000 .. multiple camera support
47 * Thanks to: the folk who've provided USB product IDs, sent in
48 * patches, and shared their sucesses!
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/signal.h>
54 #include <linux/errno.h>
55 #include <linux/miscdevice.h>
56 #include <linux/random.h>
57 #include <linux/poll.h>
58 #include <linux/init.h>
59 #include <linux/malloc.h>
60 #include <linux/module.h>
61 #undef DEBUG
62 #include <linux/usb.h>
66 /* current USB framework handles max of 16 USB devices per driver */
67 #define MAX_CAMERAS 8
69 /* USB char devs use USB_MAJOR and from USB_CAMERA_MINOR_BASE up */
70 #define USB_CAMERA_MINOR_BASE 80
73 // XXX remove packet size limit, now that bulk transfers seem fixed
75 /* Application protocol limit is 0x8002; USB has disliked that limit! */
76 #define MAX_PACKET_SIZE 0x2000 /* e.g. image downloading */
78 #define MAX_READ_RETRY 5 /* times to retry reads */
79 #define MAX_WRITE_RETRY 5 /* times to retry writes */
80 #define RETRY_TIMEOUT (HZ) /* sleep between retries */
83 /* table of cameras that work through this driver */
84 static const struct camera {
85 short idVendor;
86 short idProduct;
87 /* plus hooks for camera-specific info if needed */
88 } cameras [] = {
89 /* These have the same application level protocol */
90 { 0x040a, 0x0120 }, // Kodak DC-240
91 { 0x040a, 0x0130 }, // Kodak DC-280
93 /* These have a different application level protocol which
94 * is part of the Flashpoint "DigitaOS". That supports some
95 * non-camera devices, and some non-Kodak cameras.
96 */
97 { 0x040a, 0x0100 }, // Kodak DC-220
98 { 0x040a, 0x0110 }, // Kodak DC-260
99 { 0x040a, 0x0111 }, // Kodak DC-265
100 { 0x040a, 0x0112 }, // Kodak DC-290
101 // { 0x03f0, 0xffff }, // HP PhotoSmart C500
103 /* Other USB devices may well work here too, so long as they
104 * just stick to half duplex bulk packet exchanges. That
105 * means, among other things, no iso or interrupt endpoints.
110 struct camera_state {
111 struct usb_device *dev; /* USB device handle */
112 char inEP; /* read endpoint */
113 char outEP; /* write endpoint */
114 const struct camera *info; /* DC-240, etc */
115 int subminor; /* which minor dev #? */
116 int isActive; /* I/O taking place? */
118 /* this is non-null iff the device is open */
119 char *buf; /* buffer for I/O */
121 /* always valid */
122 wait_queue_head_t wait; /* for timed waits */
126 /* Support multiple cameras, possibly of different types. */
127 static struct camera_state *minor_data [MAX_CAMERAS];
130 static ssize_t camera_read (struct file *file,
131 char *buf, size_t len, loff_t *ppos)
133 struct camera_state *camera;
134 int retries;
136 if (len > MAX_PACKET_SIZE)
137 return -EINVAL;
139 camera = (struct camera_state *) file->private_data;
140 if (!camera->dev)
141 return -ENODEV;
142 if (camera->isActive++)
143 return -EBUSY;
145 /* Big reads are common, for image downloading. Smaller ones
146 * are also common (even "directory listing" commands don't
147 * send very much data). We preserve packet boundaries here,
148 * they matter in the application protocol.
150 for (retries = 0; retries < MAX_READ_RETRY; retries++) {
151 int count;
152 int result;
154 if (signal_pending (current)) {
155 camera->isActive = 0;
156 return -EINTR;
158 if (!camera->dev) {
159 camera->isActive = 0;
160 return -ENODEV;
163 result = usb_bulk_msg (camera->dev,
164 usb_rcvbulkpipe (camera->dev, camera->inEP),
165 camera->buf, len, &count, HZ*10);
167 dbg ("read (%d) - 0x%x %d", len, result, count);
169 if (!result) {
170 if (copy_to_user (buf, camera->buf, count))
171 return -EFAULT;
172 camera->isActive = 0;
173 return count;
175 if (result != USB_ST_TIMEOUT)
176 break;
177 interruptible_sleep_on_timeout (&camera->wait, RETRY_TIMEOUT);
179 dbg ("read (%d) - retry", len);
181 camera->isActive = 0;
182 return -EIO;
185 static ssize_t camera_write (struct file *file,
186 const char *buf, size_t len, loff_t *ppos)
188 struct camera_state *camera;
189 ssize_t bytes_written = 0;
191 if (len > MAX_PACKET_SIZE)
192 return -EINVAL;
194 camera = (struct camera_state *) file->private_data;
195 if (!camera->dev)
196 return -ENODEV;
197 if (camera->isActive++)
198 return -EBUSY;
200 /* most writes will be small: simple commands, sometimes with
201 * parameters. putting images (like borders) into the camera
202 * would be the main use of big writes.
204 while (len > 0) {
205 char *obuf = camera->buf;
206 int maxretry = MAX_WRITE_RETRY;
207 unsigned long copy_size, thistime;
209 /* it's not clear that retrying can do any good ... or that
210 * fragmenting application packets into N writes is correct.
212 thistime = copy_size = len;
213 if (copy_from_user (obuf, buf, copy_size)) {
214 bytes_written = -EFAULT;
215 break;
217 while (thistime) {
218 int result;
219 int count;
221 if (signal_pending (current)) {
222 if (!bytes_written)
223 bytes_written = -EINTR;
224 goto done;
226 if (!camera->dev) {
227 if (!bytes_written)
228 bytes_written = -ENODEV;
229 goto done;
232 result = usb_bulk_msg (camera->dev,
233 usb_sndbulkpipe (camera->dev, camera->outEP),
234 obuf, thistime, &count, HZ*10);
236 if (result)
237 dbg ("write USB err - %x", result);
239 if (count) {
240 obuf += count;
241 thistime -= count;
242 maxretry = MAX_WRITE_RETRY;
243 continue;
244 } else if (!result)
245 break;
247 if (result == USB_ST_TIMEOUT) { /* NAK - delay a bit */
248 if (!maxretry--) {
249 if (!bytes_written)
250 bytes_written = -ETIME;
251 goto done;
253 interruptible_sleep_on_timeout (&camera->wait,
254 RETRY_TIMEOUT);
255 continue;
257 if (!bytes_written)
258 bytes_written = -EIO;
259 goto done;
261 bytes_written += copy_size;
262 len -= copy_size;
263 buf += copy_size;
265 done:
266 camera->isActive = 0;
267 dbg ("wrote %d", bytes_written);
268 return bytes_written;
271 static int camera_open (struct inode *inode, struct file *file)
273 struct camera_state *camera;
274 int subminor;
276 subminor = MINOR (inode->i_rdev) - USB_CAMERA_MINOR_BASE;
277 if (subminor < 0 || subminor >= MAX_CAMERAS
278 || !(camera = minor_data [subminor])) {
279 return -ENODEV;
282 if (!(camera->buf = (char *) kmalloc (MAX_PACKET_SIZE, GFP_KERNEL))) {
283 return -ENOMEM;
286 dbg ("open");
288 /* Keep driver from being unloaded while it's in use */
289 MOD_INC_USE_COUNT;
291 camera->isActive = 0;
292 file->private_data = camera;
293 return 0;
296 static int camera_release (struct inode *inode, struct file *file)
298 struct camera_state *camera;
300 camera = (struct camera_state *) file->private_data;
301 kfree (camera->buf);
303 /* If camera was unplugged with open file ... */
304 if (!camera->dev) {
305 minor_data [camera->subminor] = NULL;
306 kfree (camera);
309 MOD_DEC_USE_COUNT;
311 dbg ("close");
313 return 0;
316 /* XXX should define some ioctls to expose camera type
317 * to applications ... what USB exposes should suffice.
318 * apps should be able to see the camera type.
320 static /* const */ struct file_operations usb_camera_fops = {
321 /* Uses GCC initializer extension; simpler to maintain */
322 read: camera_read,
323 write: camera_write,
324 open: camera_open,
325 release: camera_release,
330 static void * camera_probe(struct usb_device *dev, unsigned int ifnum)
332 int i;
333 const struct camera *camera_info = NULL;
334 struct usb_interface_descriptor *interface;
335 struct usb_endpoint_descriptor *endpoint;
336 int direction, ep;
337 struct camera_state *camera;
339 /* Is it a supported camera? */
340 for (i = 0; i < sizeof (cameras) / sizeof (struct camera); i++) {
341 if (cameras [i].idVendor != dev->descriptor.idVendor)
342 continue;
343 if (cameras [i].idProduct != dev->descriptor.idProduct)
344 continue;
345 camera_info = &cameras [i];
346 break;
348 if (camera_info == NULL)
349 return NULL;
351 /* these have one config, one interface */
352 if (dev->descriptor.bNumConfigurations != 1
353 || dev->config[0].bNumInterfaces != 1) {
354 dbg ("Bogus camera config info");
355 return NULL;
358 /* models differ in how they report themselves */
359 interface = &dev->actconfig->interface[ifnum].altsetting[0];
360 if ((interface->bInterfaceClass != USB_CLASS_PER_INTERFACE
361 && interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC)
362 || interface->bInterfaceSubClass != 0
363 || interface->bInterfaceProtocol != 0
364 || interface->bNumEndpoints != 2
366 dbg ("Bogus camera interface info");
367 return NULL;
371 /* select "subminor" number (part of a minor number) */
372 for (i = 0; i < MAX_CAMERAS; i++) {
373 if (!minor_data [i])
374 break;
376 if (i >= MAX_CAMERAS) {
377 info ("Ignoring additional USB Camera");
378 return NULL;
381 /* allocate & init camera state */
382 camera = minor_data [i] = kmalloc (sizeof *camera, GFP_KERNEL);
383 if (!camera) {
384 err ("no memory!");
385 return NULL;
387 camera->info = camera_info;
388 camera->subminor = i;
389 camera->isActive = 0;
390 camera->buf = NULL;
391 init_waitqueue_head (&camera->wait);
392 info ("USB Camera #%d connected", camera->subminor);
395 /* get input and output endpoints (either order) */
396 endpoint = interface->endpoint;
397 camera->outEP = camera->inEP = -1;
399 ep = endpoint [0].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
400 direction = endpoint [0].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
401 if (direction == USB_DIR_IN)
402 camera->inEP = ep;
403 else
404 camera->outEP = ep;
406 ep = endpoint [1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
407 direction = endpoint [1].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
408 if (direction == USB_DIR_IN)
409 camera->inEP = ep;
410 else
411 camera->outEP = ep;
413 if (camera->outEP == -1 || camera->inEP == -1
414 || endpoint [0].bmAttributes != USB_ENDPOINT_XFER_BULK
415 || endpoint [1].bmAttributes != USB_ENDPOINT_XFER_BULK
417 dbg ("Bogus endpoints");
418 goto error;
422 if (usb_set_configuration (dev, dev->config[0].bConfigurationValue)) {
423 err ("Failed usb_set_configuration");
424 goto error;
427 camera->dev = dev;
428 return camera;
430 error:
431 minor_data [camera->subminor] = NULL;
432 kfree (camera);
433 return NULL;
436 static void camera_disconnect(struct usb_device *dev, void *ptr)
438 struct camera_state *camera = (struct camera_state *) ptr;
439 int subminor = camera->subminor;
441 /* If camera's not opened, we can clean up right away.
442 * Else apps see a disconnect on next I/O; the release cleans.
444 if (!camera->buf) {
445 minor_data [subminor] = NULL;
446 kfree (camera);
447 } else
448 camera->dev = NULL;
450 info ("USB Camera #%d disconnected", subminor);
453 static /* const */ struct usb_driver camera_driver = {
454 "dc2xx",
455 camera_probe,
456 camera_disconnect,
457 { NULL, NULL },
458 &usb_camera_fops,
459 USB_CAMERA_MINOR_BASE
463 int __init usb_dc2xx_init(void)
465 if (usb_register (&camera_driver) < 0)
466 return -1;
467 return 0;
470 void __exit usb_dc2xx_cleanup(void)
472 usb_deregister (&camera_driver);
476 MODULE_AUTHOR("David Brownell, david-b@pacbell.net");
477 MODULE_DESCRIPTION("USB Camera Driver for Kodak DC-2xx series cameras");
479 module_init (usb_dc2xx_init);
480 module_exit (usb_dc2xx_cleanup);