~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/usb/dc2xx.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 1999-2000 by David Brownell <dbrownell@users.sourceforge.net>
  3  *
  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.
  8  *
  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.
 13  *
 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.
 17  */
 18  
 19  
 20 /*
 21  * USB driver for Kodak DC-2XX series digital still cameras
 22  *
 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!
 26  *
 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.
 30  */
 31 
 32 /*
 33  * HISTORY
 34  *
 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
 46  * 12 Aug, 2000 .. add some real locking, remove an Oops
 47  * 10 Oct, 2000 .. usb_device_id table created. 
 48  * 01 Nov, 2000 .. usb_device_id support added by Adam J. Richter
 49  *
 50  * Thanks to:  the folk who've provided USB product IDs, sent in
 51  * patches, and shared their sucesses!
 52  */
 53 
 54 #include <linux/config.h>
 55 #include <linux/kernel.h>
 56 #include <linux/sched.h>
 57 #include <linux/signal.h>
 58 #include <linux/errno.h>
 59 #include <linux/miscdevice.h>
 60 #include <linux/random.h>
 61 #include <linux/poll.h>
 62 #include <linux/init.h>
 63 #include <linux/malloc.h>
 64 #include <linux/module.h>
 65 
 66 #ifdef CONFIG_USB_DEBUG
 67         #define DEBUG
 68 #else
 69         #undef DEBUG
 70 #endif
 71 #include <linux/usb.h>
 72 
 73 
 74 
 75 /* current USB framework handles max of 16 USB devices per driver */
 76 #define MAX_CAMERAS             16
 77 
 78 /* USB char devs use USB_MAJOR and from USB_CAMERA_MINOR_BASE up */
 79 #define USB_CAMERA_MINOR_BASE   80
 80 
 81 
 82 // XXX remove packet size limit, now that bulk transfers seem fixed
 83 
 84 /* Application protocol limit is 0x8002; USB has disliked that limit! */
 85 #define MAX_PACKET_SIZE         0x2000          /* e.g. image downloading */
 86 
 87 #define MAX_READ_RETRY          5               /* times to retry reads */
 88 #define MAX_WRITE_RETRY         5               /* times to retry writes */
 89 #define RETRY_TIMEOUT           (HZ)            /* sleep between retries */
 90 
 91 
 92 /* table of cameras that work through this driver */
 93 static struct usb_device_id camera_table [] = {
 94         /* These have the same application level protocol */  
 95         { USB_DEVICE(0x040a, 0x0120) },         // Kodak DC-240
 96         { USB_DEVICE(0x040a, 0x0130) },         // Kodak DC-280
 97         { USB_DEVICE(0x040a, 0x0131) },         // Kodak DC-5000
 98         { USB_DEVICE(0x040a, 0x0132) },         // Kodak DC-3400
 99 
100         /* These have a different application level protocol which
101          * is part of the Flashpoint "DigitaOS".  That supports some
102          * non-camera devices, and some non-Kodak cameras.
103          */  
104         { USB_DEVICE(0x040a, 0x0100) },         // Kodak DC-220
105         { USB_DEVICE(0x040a, 0x0110) },         // Kodak DC-260
106         { USB_DEVICE(0x040a, 0x0111) },         // Kodak DC-265
107         { USB_DEVICE(0x040a, 0x0112) },         // Kodak DC-290
108         { USB_DEVICE(0xf003, 0x6002) },         // HP PhotoSmart C500
109 
110         /* Other USB devices may well work here too, so long as they
111          * just stick to half duplex bulk packet exchanges.  That
112          * means, among other things, no iso or interrupt endpoints.
113          */
114 
115         { }                                     /* Terminating entry */
116 };
117 
118 MODULE_DEVICE_TABLE (usb, camera_table);
119 
120 
121 struct camera_state {
122         struct usb_device       *dev;           /* USB device handle */
123         int                     inEP;           /* read endpoint */
124         int                     outEP;          /* write endpoint */
125         const struct usb_device_id      *info;  /* DC-240, etc */
126         int                     subminor;       /* which minor dev #? */
127         struct semaphore        sem;            /* locks this struct */
128 
129         /* this is non-null iff the device is open */
130         char                    *buf;           /* buffer for I/O */
131 
132         /* always valid */
133         wait_queue_head_t       wait;           /* for timed waits */
134 };
135 
136 
137 /* Support multiple cameras, possibly of different types.  */
138 static struct camera_state *minor_data [MAX_CAMERAS];
139 
140 /* make this an rwlock if contention becomes an issue */
141 static DECLARE_MUTEX (state_table_mutex);
142 
143 static ssize_t camera_read (struct file *file,
144         char *buf, size_t len, loff_t *ppos)
145 {
146         struct camera_state     *camera;
147         int                     retries;
148         int                     retval = 0;
149 
150         if (len > MAX_PACKET_SIZE)
151                 return -EINVAL;
152 
153         camera = (struct camera_state *) file->private_data;
154         down (&camera->sem);
155         if (!camera->dev) {
156                 up (&camera->sem);
157                 return -ENODEV;
158         }
159 
160         /* Big reads are common, for image downloading.  Smaller ones
161          * are also common (even "directory listing" commands don't
162          * send very much data).  We preserve packet boundaries here,
163          * they matter in the application protocol.
164          */
165         for (retries = 0; retries < MAX_READ_RETRY; retries++) {
166                 int                     count;
167 
168                 if (signal_pending (current)) {
169                         retval = -EINTR;
170                         break;
171                 }
172 
173                 retval = usb_bulk_msg (camera->dev,
174                           usb_rcvbulkpipe (camera->dev, camera->inEP),
175                           camera->buf, len, &count, HZ*10);
176 
177                 dbg ("read (%d) - 0x%x %d", len, retval, count);
178 
179                 if (!retval) {
180                         if (copy_to_user (buf, camera->buf, count))
181                                 retval = -EFAULT;
182                         else
183                                 retval = count;
184                         break;
185                 }
186                 if (retval != USB_ST_TIMEOUT)
187                         break;
188                 interruptible_sleep_on_timeout (&camera->wait, RETRY_TIMEOUT);
189 
190                 dbg ("read (%d) - retry", len);
191         }
192         up (&camera->sem);
193         return retval;
194 }
195 
196 static ssize_t camera_write (struct file *file,
197         const char *buf, size_t len, loff_t *ppos)
198 {
199         struct camera_state     *camera;
200         ssize_t                 bytes_written = 0;
201 
202         if (len > MAX_PACKET_SIZE)
203                 return -EINVAL;
204 
205         camera = (struct camera_state *) file->private_data;
206         down (&camera->sem);
207         if (!camera->dev) {
208                 up (&camera->sem);
209                 return -ENODEV;
210         }
211         
212         /* most writes will be small: simple commands, sometimes with
213          * parameters.  putting images (like borders) into the camera
214          * would be the main use of big writes.
215          */
216         while (len > 0) {
217                 char            *obuf = camera->buf;
218                 int             maxretry = MAX_WRITE_RETRY;
219                 unsigned long   copy_size, thistime;
220 
221                 /* it's not clear that retrying can do any good ... or that
222                  * fragmenting application packets into N writes is correct.
223                  */
224                 thistime = copy_size = len;
225                 if (copy_from_user (obuf, buf, copy_size)) {
226                         bytes_written = -EFAULT;
227                         break;
228                 }
229                 while (thistime) {
230                         int             result;
231                         int             count;
232 
233                         if (signal_pending (current)) {
234                                 if (!bytes_written)
235                                         bytes_written = -EINTR;
236                                 goto done;
237                         }
238 
239                         result = usb_bulk_msg (camera->dev,
240                                  usb_sndbulkpipe (camera->dev, camera->outEP),
241                                  obuf, thistime, &count, HZ*10);
242 
243                         if (result)
244                                 dbg ("write USB err - %d", result);
245 
246                         if (count) {
247                                 obuf += count;
248                                 thistime -= count;
249                                 maxretry = MAX_WRITE_RETRY;
250                                 continue;
251                         } else if (!result)
252                                 break;
253                                 
254                         if (result == USB_ST_TIMEOUT) { /* NAK - delay a bit */
255                                 if (!maxretry--) {
256                                         if (!bytes_written)
257                                                 bytes_written = -ETIME;
258                                         goto done;
259                                 }
260                                 interruptible_sleep_on_timeout (&camera->wait,
261                                         RETRY_TIMEOUT);
262                                 continue;
263                         } 
264                         if (!bytes_written)
265                                 bytes_written = -EIO;
266                         goto done;
267                 }
268                 bytes_written += copy_size;
269                 len -= copy_size;
270                 buf += copy_size;
271         }
272 done:
273         up (&camera->sem);
274         dbg ("wrote %d", bytes_written); 
275         return bytes_written;
276 }
277 
278 static int camera_open (struct inode *inode, struct file *file)
279 {
280         struct camera_state     *camera = NULL;
281         int                     subminor;
282         int                     value = 0;
283 
284         down (&state_table_mutex);
285         subminor = MINOR (inode->i_rdev) - USB_CAMERA_MINOR_BASE;
286         if (subminor < 0 || subminor >= MAX_CAMERAS
287                         || !(camera = minor_data [subminor])) {
288                 up (&state_table_mutex);
289                 return -ENODEV;
290         }
291         down (&camera->sem);
292         up (&state_table_mutex);
293 
294         if (camera->buf) {
295                 value = -EBUSY;
296                 goto done;
297         }
298 
299         if (!(camera->buf = (char *) kmalloc (MAX_PACKET_SIZE, GFP_KERNEL))) {
300                 value = -ENOMEM;
301                 goto done;
302         }
303 
304         dbg ("open #%d", subminor); 
305 
306         file->private_data = camera;
307 done:
308         up (&camera->sem);
309         return value;
310 }
311 
312 static int camera_release (struct inode *inode, struct file *file)
313 {
314         struct camera_state     *camera;
315         int                     subminor;
316 
317         camera = (struct camera_state *) file->private_data;
318         down (&state_table_mutex);
319         down (&camera->sem);
320 
321         if (camera->buf) {
322                 kfree (camera->buf);
323                 camera->buf = 0;
324         }
325         subminor = camera->subminor;
326 
327         /* If camera was unplugged with open file ... */
328         if (!camera->dev) {
329                 minor_data [subminor] = NULL;
330                 kfree (camera);
331         }
332         up (&camera->sem);
333         up (&state_table_mutex);
334 
335         dbg ("close #%d", subminor); 
336 
337         return 0;
338 }
339 
340         /* XXX should define some ioctls to expose camera type
341          * to applications ... what USB exposes should suffice.
342          * apps should be able to see the camera type.
343          */
344 static /* const */ struct file_operations usb_camera_fops = {
345             /* Uses GCC initializer extension; simpler to maintain */
346         owner:          THIS_MODULE,
347         read:           camera_read,
348         write:          camera_write,
349         open:           camera_open,
350         release:        camera_release,
351 };
352 
353 
354 
355 static void *
356 camera_probe (struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *camera_info)
357 {
358         int                             i;
359         struct usb_interface_descriptor *interface;
360         struct usb_endpoint_descriptor  *endpoint;
361         int                             direction, ep;
362         struct camera_state             *camera = NULL;
363 
364         /* these have one config, one interface */
365         if (dev->descriptor.bNumConfigurations != 1
366                         || dev->config[0].bNumInterfaces != 1) {
367                 dbg ("Bogus camera config info");
368                 return NULL;
369         }
370 
371         /* models differ in how they report themselves */
372         interface = &dev->actconfig->interface[ifnum].altsetting[0];
373         if ((interface->bInterfaceClass != USB_CLASS_PER_INTERFACE
374                 && interface->bInterfaceClass != USB_CLASS_VENDOR_SPEC)
375                         || interface->bInterfaceSubClass != 0
376                         || interface->bInterfaceProtocol != 0
377                         || interface->bNumEndpoints != 2
378                         ) {
379                 dbg ("Bogus camera interface info");
380                 return NULL;
381         }
382 
383 
384         /* select "subminor" number (part of a minor number) */
385         down (&state_table_mutex);
386         for (i = 0; i < MAX_CAMERAS; i++) {
387                 if (!minor_data [i])
388                         break;
389         }
390         if (i >= MAX_CAMERAS) {
391                 info ("Ignoring additional USB Camera");
392                 up (&state_table_mutex);
393                 goto bye;
394         }
395 
396         /* allocate & init camera state */
397         camera = minor_data [i] = kmalloc (sizeof *camera, GFP_KERNEL);
398         if (!camera) {
399                 err ("no memory!");
400                 up (&state_table_mutex);
401                 goto bye;
402         }
403 
404         init_MUTEX (&camera->sem);
405         camera->info = camera_info;
406         camera->subminor = i;
407         camera->buf = NULL;
408         init_waitqueue_head (&camera->wait);
409 
410 
411         /* get input and output endpoints (either order) */
412         endpoint = interface->endpoint;
413         camera->outEP = camera->inEP =  -1;
414 
415         ep = endpoint [0].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
416         direction = endpoint [0].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
417         if (direction == USB_DIR_IN)
418                 camera->inEP = ep;
419         else
420                 camera->outEP = ep;
421 
422         ep = endpoint [1].bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
423         direction = endpoint [1].bEndpointAddress & USB_ENDPOINT_DIR_MASK;
424         if (direction == USB_DIR_IN)
425                 camera->inEP = ep;
426         else
427                 camera->outEP = ep;
428 
429         if (camera->outEP == -1 || camera->inEP == -1
430                         || endpoint [0].bmAttributes != USB_ENDPOINT_XFER_BULK
431                         || endpoint [1].bmAttributes != USB_ENDPOINT_XFER_BULK
432                         ) {
433                 dbg ("Bogus endpoints");
434                 goto error;
435         }
436 
437         info ("USB Camera #%d connected, major/minor %d/%d", camera->subminor,
438                 USB_MAJOR, USB_CAMERA_MINOR_BASE + camera->subminor);
439 
440         camera->dev = dev;
441         usb_inc_dev_use (dev);
442         goto bye;
443 
444 error:
445         minor_data [camera->subminor] = NULL;
446         kfree (camera);
447         camera = NULL;
448 bye:
449         up (&state_table_mutex);
450         return camera;
451 }
452 
453 static void camera_disconnect(struct usb_device *dev, void *ptr)
454 {
455         struct camera_state     *camera = (struct camera_state *) ptr;
456         int                     subminor = camera->subminor;
457 
458         down (&state_table_mutex);
459         down (&camera->sem);
460 
461         /* If camera's not opened, we can clean up right away.
462          * Else apps see a disconnect on next I/O; the release cleans.
463          */
464         if (!camera->buf) {
465                 minor_data [subminor] = NULL;
466                 kfree (camera);
467         } else
468                 camera->dev = NULL;
469 
470         info ("USB Camera #%d disconnected", subminor);
471         usb_dec_dev_use (dev);
472 
473         up (&camera->sem);
474         up (&state_table_mutex);
475 }
476 
477 static /* const */ struct usb_driver camera_driver = {
478         name:           "dc2xx",
479 
480         id_table:       camera_table,
481         probe:          camera_probe,
482         disconnect:     camera_disconnect,
483 
484         fops:           &usb_camera_fops,
485         minor:          USB_CAMERA_MINOR_BASE
486 };
487 
488 
489 int __init usb_dc2xx_init(void)
490 {
491         if (usb_register (&camera_driver) < 0)
492                 return -1;
493         return 0;
494 }
495 
496 void __exit usb_dc2xx_cleanup(void)
497 {
498         usb_deregister (&camera_driver);
499 }
500 
501 
502 MODULE_AUTHOR("David Brownell, <dbrownell@users.sourceforge.net>");
503 MODULE_DESCRIPTION("USB Camera Driver for Kodak DC-2xx series cameras");
504 
505 module_init (usb_dc2xx_init);
506 module_exit (usb_dc2xx_cleanup);
507 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.