Mercurial > code > home > repos > light9
annotate dmx_usb_module/dmx_usb.c @ 634:7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
Ignore-this: 7d3167e0c028a60f152114a8addcb18
author | drewp@bigasterisk.com |
---|---|
date | Wed, 15 Jun 2011 07:54:58 +0000 |
parents | 3648a427e9a0 |
children | 01d330152a18 |
rev | line source |
---|---|
355 | 1 /* |
2 * DMX USB driver | |
3 * | |
4 * Copyright (C) 2004,2006 Erwin Rol (erwin@erwinrol.com) | |
5 * | |
6 * This driver is based on the usb-skeleton driver; | |
7 * | |
8 * Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com) | |
9 * | |
10 * This program is free software; you can redistribute it and/or | |
11 * modify it under the terms of the GNU General Public License as | |
12 * published by the Free Software Foundation, version 2. | |
13 * | |
14 * $Id: dmx_usb.c 41 2004-09-14 23:35:25Z erwin $ | |
15 */ | |
16 | |
17 #include <linux/kernel.h> | |
18 #include <linux/errno.h> | |
19 #include <linux/init.h> | |
20 #include <linux/slab.h> | |
21 #include <linux/module.h> | |
22 #include <linux/smp_lock.h> | |
23 #include <linux/completion.h> | |
24 #include <asm/uaccess.h> | |
25 #include <linux/usb.h> | |
26 #include <linux/version.h> | |
27 | |
28 #include "dmx_usb.h" | |
29 | |
30 #ifdef CONFIG_USB_DEBUG | |
31 static int debug = 1; | |
32 #else | |
33 static int debug; | |
34 #endif | |
35 | |
36 /* Use our own dbg macro */ | |
37 #undef dbg | |
38 #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0) | |
39 | |
40 | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
41 // from http://www.linuxinsight.com/vmware-workstation-7.1.3-runs-great-on-linux-kernel-2.6.37.html |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
42 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37) |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
43 #define DECLARE_MUTEX(_m) DEFINE_SEMAPHORE(_m) |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
44 #define init_MUTEX(_m) sema_init(_m, 1) |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
45 #endif |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
46 |
355 | 47 /* Version Information */ |
48 #define DRIVER_VERSION "v0.1.20060816" | |
49 #define DRIVER_AUTHOR "Erwin Rol, erwin@erwinrol.com" | |
50 #define DRIVER_DESC "DMX USB Driver" | |
51 | |
52 /* Module parameters */ | |
53 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) ) | |
54 MODULE_PARM(debug, "i"); | |
55 MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
56 #else | |
57 module_param(debug, bool, S_IRUGO | S_IWUSR); | |
58 MODULE_PARM_DESC(debug, "Debug enabled or not"); | |
59 #endif | |
60 | |
61 static struct usb_device_id dmx_usb_table [] = { | |
62 { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_PID, 0x400, 0xffff) }, | |
63 { USB_DEVICE_VER(FTDI_VID, FTDI_8U232AM_ALT_PID, 0x400, 0xffff) }, | |
64 { } /* Terminating entry */ | |
65 }; | |
66 | |
67 MODULE_DEVICE_TABLE (usb, dmx_usb_table); | |
68 | |
69 /* Get a minor range for your devices from the usb maintainer */ | |
70 #define DMX_USB_MINOR_BASE 192 | |
71 | |
72 /* Structure to hold all of our device specific stuff */ | |
73 struct dmx_usb_device { | |
74 struct usb_device * udev; /* save off the usb device pointer */ | |
75 struct usb_interface * interface; /* the interface for this device */ | |
76 unsigned char minor; /* the starting minor number for this device */ | |
77 unsigned char num_ports; /* the number of ports this device has */ | |
78 char num_interrupt_in; /* number of interrupt in endpoints we have */ | |
79 char num_bulk_in; /* number of bulk in endpoints we have */ | |
80 char num_bulk_out; /* number of bulk out endpoints we have */ | |
81 | |
82 unsigned char * bulk_in_buffer; /* the buffer to receive data */ | |
83 size_t bulk_in_size; /* the size of the receive buffer */ | |
84 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */ | |
85 | |
86 unsigned char * bulk_out_buffer; /* the buffer to send data */ | |
87 size_t bulk_out_size; /* the size of the send buffer */ | |
88 struct urb * write_urb; /* the urb used to send data */ | |
89 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */ | |
90 atomic_t write_busy; /* true iff write urb is busy */ | |
91 struct completion write_finished; /* wait for the write to finish */ | |
92 | |
93 int open; /* if the port is open or not */ | |
94 int present; /* if the device is not disconnected */ | |
95 struct semaphore sem; /* locks this structure */ | |
96 }; | |
97 | |
98 | |
99 /* prevent races between open() and disconnect() */ | |
100 static DECLARE_MUTEX (disconnect_sem); | |
101 | |
102 /* local function prototypes */ | |
103 //static ssize_t dmx_usb_read (struct file *file, char *buffer, size_t count, loff_t *ppos); | |
104 static ssize_t dmx_usb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos); | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
105 static int dmx_usb_ioctl (struct file *file, unsigned int cmd, unsigned long arg); |
355 | 106 static int dmx_usb_open (struct inode *inode, struct file *file); |
107 static int dmx_usb_release (struct inode *inode, struct file *file); | |
108 | |
109 static int dmx_usb_probe (struct usb_interface *interface, const struct usb_device_id *id); | |
110 static void dmx_usb_disconnect (struct usb_interface *interface); | |
111 | |
112 static void dmx_usb_write_bulk_callback (struct urb *urb, struct pt_regs *regs); | |
113 | |
114 static struct file_operations dmx_usb_fops = { | |
115 /* | |
116 * The owner field is part of the module-locking | |
117 * mechanism. The idea is that the kernel knows | |
118 * which module to increment the use-counter of | |
119 * BEFORE it calls the device's open() function. | |
120 * This also means that the kernel can decrement | |
121 * the use-counter again before calling release() | |
122 * or should the open() function fail. | |
123 */ | |
124 .owner = THIS_MODULE, | |
125 | |
126 /* .read = dmx_usb_read, */ | |
127 .write = dmx_usb_write, | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
128 .unlocked_ioctl = dmx_usb_ioctl, |
355 | 129 .open = dmx_usb_open, |
130 .release = dmx_usb_release, | |
131 }; | |
132 | |
133 /* | |
134 * usb class driver info in order to get a minor number from the usb core, | |
135 * and to have the device registered with devfs and the driver core | |
136 */ | |
137 static struct usb_class_driver dmx_usb_class = { | |
138 .name = "usb/dmx%d", | |
139 .fops = &dmx_usb_fops, | |
140 .minor_base = DMX_USB_MINOR_BASE, | |
141 }; | |
142 | |
143 /* usb specific object needed to register this driver with the usb subsystem */ | |
144 static struct usb_driver dmx_usb_driver = { | |
145 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,6,16) ) | |
146 .owner = THIS_MODULE, | |
147 #endif | |
148 .name = "dmx_usb", | |
149 .probe = dmx_usb_probe, | |
150 .disconnect = dmx_usb_disconnect, | |
151 .id_table = dmx_usb_table, | |
152 }; | |
153 | |
154 | |
155 /** | |
156 */ | |
157 static inline void dmx_usb_debug_data (const char *function, int size, const unsigned char *data) | |
158 { | |
159 int i; | |
160 | |
161 if (!debug) | |
162 return; | |
163 | |
164 printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", | |
165 function, size); | |
166 for (i = 0; i < size; ++i) { | |
167 printk ("%.2x ", data[i]); | |
168 } | |
169 printk ("\n"); | |
170 } | |
171 | |
172 static __u32 dmx_usb_baud_to_divisor(int baud) | |
173 { | |
174 static const unsigned char divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; | |
175 __u32 divisor; | |
176 int divisor3 = 48000000 / 2 / baud; // divisor shifted 3 bits to the left | |
177 divisor = divisor3 >> 3; | |
178 divisor |= (__u32)divfrac[divisor3 & 0x7] << 14; | |
179 /* Deal with special cases for highest baud rates. */ | |
180 if (divisor == 1) divisor = 0; else // 1.0 | |
181 if (divisor == 0x4001) divisor = 1; // 1.5 | |
182 return divisor; | |
183 } | |
184 | |
185 static int dmx_usb_set_speed(struct dmx_usb_device* dev) | |
186 { | |
187 char *buf; | |
188 __u16 urb_value; | |
189 __u16 urb_index; | |
190 __u32 urb_index_value; | |
191 int rv; | |
192 | |
193 buf = kmalloc(1, GFP_NOIO); | |
194 if (!buf) | |
195 return -ENOMEM; | |
196 | |
197 urb_index_value = dmx_usb_baud_to_divisor(250000); | |
198 urb_value = (__u16)urb_index_value; | |
199 urb_index = (__u16)(urb_index_value >> 16); | |
200 | |
201 rv = usb_control_msg(dev->udev, | |
202 usb_sndctrlpipe(dev->udev, 0), | |
203 FTDI_SIO_SET_BAUDRATE_REQUEST, | |
204 FTDI_SIO_SET_BAUDRATE_REQUEST_TYPE, | |
205 urb_value, urb_index, | |
206 buf, 0, HZ*10); | |
207 | |
208 kfree(buf); | |
209 return rv; | |
210 } | |
211 | |
212 static int dmx_usb_setup(struct dmx_usb_device* dev) | |
213 { | |
214 __u16 urb_value; | |
215 char buf[1]; | |
216 | |
217 urb_value = FTDI_SIO_SET_DATA_STOP_BITS_2 | FTDI_SIO_SET_DATA_PARITY_NONE; | |
218 urb_value |= 8; // number of data bits | |
219 | |
220 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), | |
221 FTDI_SIO_SET_DATA_REQUEST, | |
222 FTDI_SIO_SET_DATA_REQUEST_TYPE, | |
223 urb_value , 0, | |
224 buf, 0, HZ*10) < 0) { | |
225 err("%s FAILED to set databits/stopbits/parity", __FUNCTION__); | |
226 } | |
227 | |
228 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), | |
229 FTDI_SIO_SET_FLOW_CTRL_REQUEST, | |
230 FTDI_SIO_SET_FLOW_CTRL_REQUEST_TYPE, | |
231 0, 0, | |
232 buf, 0, HZ*10) < 0) { | |
233 err("%s error from disable flowcontrol urb", __FUNCTION__); | |
234 } | |
235 | |
236 dmx_usb_set_speed(dev); | |
237 | |
238 return 0; | |
239 } | |
240 | |
241 static void dmx_usb_set_break(struct dmx_usb_device* dev, int break_state) | |
242 { | |
243 __u16 urb_value = FTDI_SIO_SET_DATA_STOP_BITS_2 | FTDI_SIO_SET_DATA_PARITY_NONE | 8; | |
244 | |
245 char buf[2]; | |
246 | |
247 if (break_state) { | |
248 urb_value |= FTDI_SIO_SET_BREAK; | |
249 } | |
250 | |
251 if (usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0), | |
252 FTDI_SIO_SET_DATA_REQUEST, | |
253 FTDI_SIO_SET_DATA_REQUEST_TYPE, | |
254 urb_value , 0, | |
255 buf, 2, HZ*10) < 0) { | |
256 err("%s FAILED to enable/disable break state (state was %d)", __FUNCTION__,break_state); | |
257 } | |
258 | |
259 | |
260 dbg("%s break state is %d - urb is %d", __FUNCTION__,break_state, urb_value); | |
261 } | |
262 | |
263 /** | |
264 */ | |
265 static inline void dmx_usb_delete (struct dmx_usb_device *dev) | |
266 { | |
267 kfree (dev->bulk_in_buffer); | |
633
3648a427e9a0
switch to usb_free_coherent and usb_alloc_coherent for new linux kernel
Drew Perttula <drewp@bigasterisk.com>
parents:
555
diff
changeset
|
268 usb_free_coherent (dev->udev, dev->bulk_out_size, |
355 | 269 dev->bulk_out_buffer, |
270 dev->write_urb->transfer_dma); | |
271 usb_free_urb (dev->write_urb); | |
272 kfree (dev); | |
273 } | |
274 | |
275 | |
276 /** | |
277 */ | |
278 static int dmx_usb_open (struct inode *inode, struct file *file) | |
279 { | |
280 struct dmx_usb_device *dev = NULL; | |
281 struct usb_interface *interface; | |
282 int subminor; | |
283 int retval = 0; | |
284 | |
285 dbg("%s", __FUNCTION__); | |
286 | |
287 subminor = iminor(inode); | |
288 | |
289 /* prevent disconnects */ | |
290 down (&disconnect_sem); | |
291 | |
292 interface = usb_find_interface (&dmx_usb_driver, subminor); | |
293 if (!interface) { | |
294 err ("%s - error, can't find device for minor %d", | |
295 __FUNCTION__, subminor); | |
296 retval = -ENODEV; | |
297 goto exit_no_device; | |
298 } | |
299 | |
300 dev = usb_get_intfdata(interface); | |
301 if (!dev) { | |
302 retval = -ENODEV; | |
303 goto exit_no_device; | |
304 } | |
305 | |
306 /* lock this device */ | |
307 down (&dev->sem); | |
308 | |
309 /* increment our usage count for the driver */ | |
310 ++dev->open; | |
311 | |
312 /* save our object in the file's private structure */ | |
313 file->private_data = dev; | |
314 | |
315 /* unlock this device */ | |
316 up (&dev->sem); | |
317 | |
318 exit_no_device: | |
319 up (&disconnect_sem); | |
320 return retval; | |
321 } | |
322 | |
323 | |
324 /** | |
325 */ | |
326 static int dmx_usb_release (struct inode *inode, struct file *file) | |
327 { | |
328 struct dmx_usb_device *dev; | |
329 int retval = 0; | |
330 | |
331 dev = (struct dmx_usb_device *)file->private_data; | |
332 if (dev == NULL) { | |
333 dbg ("%s - object is NULL", __FUNCTION__); | |
334 return -ENODEV; | |
335 } | |
336 | |
337 dbg("%s - minor %d", __FUNCTION__, dev->minor); | |
338 | |
339 /* lock our device */ | |
340 down (&dev->sem); | |
341 | |
342 if (dev->open <= 0) { | |
343 dbg ("%s - device not opened", __FUNCTION__); | |
344 retval = -ENODEV; | |
345 goto exit_not_opened; | |
346 } | |
347 | |
348 /* wait for any bulk writes that might be going on to finish up */ | |
349 if (atomic_read (&dev->write_busy)) | |
350 wait_for_completion (&dev->write_finished); | |
351 | |
352 --dev->open; | |
353 | |
354 if (!dev->present && !dev->open) { | |
355 /* the device was unplugged before the file was released */ | |
356 up (&dev->sem); | |
357 dmx_usb_delete (dev); | |
358 return 0; | |
359 } | |
360 | |
361 exit_not_opened: | |
362 up (&dev->sem); | |
363 | |
364 return retval; | |
365 } | |
366 | |
367 #if 0 | |
368 | |
369 Read is not yet supported | |
370 | |
371 /** | |
372 */ | |
373 static ssize_t dmx_usb_read (struct file *file, char *buffer, size_t count, loff_t *ppos) | |
374 { | |
375 struct dmx_usb_device *dev; | |
376 int retval = 0; | |
377 int bytes_read; | |
378 | |
379 dev = (struct dmx_usb_device *)file->private_data; | |
380 | |
381 dbg("%s - minor %d, count = %Zd", __FUNCTION__, dev->minor, count); | |
382 | |
383 /* lock this object */ | |
384 down (&dev->sem); | |
385 | |
386 /* verify that the device wasn't unplugged */ | |
387 if (!dev->present) { | |
388 up (&dev->sem); | |
389 return -ENODEV; | |
390 } | |
391 | |
392 /* do a blocking bulk read to get data from the device */ | |
393 retval = usb_bulk_msg (dev->udev, | |
394 usb_rcvbulkpipe (dev->udev, | |
395 dev->bulk_in_endpointAddr), | |
396 dev->bulk_in_buffer, | |
397 min (dev->bulk_in_size, count), | |
398 &bytes_read, HZ*10); | |
399 | |
400 /* if the read was successful, copy the data to userspace */ | |
401 if (!retval) { | |
402 if (copy_to_user (buffer, dev->bulk_in_buffer+2, bytes_read-2)) | |
403 retval = -EFAULT; | |
404 else | |
405 retval = bytes_read; | |
406 } | |
407 | |
408 /* unlock the device */ | |
409 up (&dev->sem); | |
410 return retval; | |
411 } | |
412 | |
413 #endif | |
414 | |
415 static __u16 dmx_usb_get_status(struct dmx_usb_device* dev) | |
416 { | |
417 int retval = 0; | |
418 int count = 0; | |
419 __u16 buf; | |
420 | |
421 retval = usb_bulk_msg (dev->udev, | |
422 usb_rcvbulkpipe (dev->udev, dev->bulk_in_endpointAddr), | |
423 &buf, 2, &count, HZ*10); | |
424 | |
425 if (retval) | |
426 return 0; | |
427 | |
428 return buf; | |
429 } | |
430 | |
431 | |
432 | |
433 /** | |
434 * dmx_usb_write | |
435 * | |
436 * A device driver has to decide how to report I/O errors back to the | |
437 * user. The safest course is to wait for the transfer to finish before | |
438 * returning so that any errors will be reported reliably. dmx_usb_read() | |
439 * works like this. But waiting for I/O is slow, so many drivers only | |
440 * check for errors during I/O initiation and do not report problems | |
441 * that occur during the actual transfer. That's what we will do here. | |
442 * | |
443 * A driver concerned with maximum I/O throughput would use double- | |
444 * buffering: Two urbs would be devoted to write transfers, so that | |
445 * one urb could always be active while the other was waiting for the | |
446 * user to send more data. | |
447 */ | |
448 static ssize_t dmx_usb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos) | |
449 { | |
450 struct dmx_usb_device *dev; | |
451 ssize_t bytes_written = 0; | |
452 int retval = 0; | |
453 __u16 stat; | |
454 | |
455 dev = (struct dmx_usb_device *)file->private_data; | |
456 | |
457 dbg("%s - minor %d, count = %Zd", __FUNCTION__, dev->minor, count); | |
458 | |
459 /* lock this object */ | |
460 down (&dev->sem); | |
461 | |
462 /* verify that the device wasn't unplugged */ | |
463 if (!dev->present) { | |
464 retval = -ENODEV; | |
465 goto exit; | |
466 } | |
467 | |
468 /* verify that we actually have some data to write */ | |
469 if (count == 0) { | |
470 dbg("%s - write request of 0 bytes", __FUNCTION__); | |
471 goto exit; | |
472 } | |
473 | |
474 /* wait for a previous write to finish up; we don't use a timeout | |
475 * and so a nonresponsive device can delay us indefinitely. | |
476 */ | |
477 if (atomic_read (&dev->write_busy)) | |
478 wait_for_completion (&dev->write_finished); | |
479 | |
480 /* we can only write as much as our buffer will hold */ | |
481 bytes_written = min (dev->bulk_out_size, count); | |
482 | |
483 /* copy the data from userspace into our transfer buffer; | |
484 * this is the only copy required. | |
485 */ | |
486 if (copy_from_user(dev->write_urb->transfer_buffer, buffer, | |
487 bytes_written)) { | |
488 retval = -EFAULT; | |
489 goto exit; | |
490 } | |
491 | |
492 dmx_usb_debug_data (__FUNCTION__, bytes_written, | |
493 dev->write_urb->transfer_buffer); | |
494 | |
495 /* this urb was already set up, except for this write size */ | |
496 dev->write_urb->transfer_buffer_length = bytes_written; | |
497 | |
498 /* Poll the device to see if the transmit buffer is empty */ | |
499 do { | |
500 stat = dmx_usb_get_status(dev); | |
501 if (stat == 0) { | |
502 retval = -EFAULT; | |
503 goto exit; | |
504 } | |
505 } while ( (stat & ((FTDI_RS_TEMT) << 8) ) == 0 ) ; | |
506 | |
507 /* the transmit buffer is empty, now toggle the break */ | |
508 dmx_usb_set_break(dev, 1); | |
509 dmx_usb_set_break(dev, 0); | |
510 | |
511 /* send the data out the bulk port */ | |
512 /* a character device write uses GFP_KERNEL, | |
513 unless a spinlock is held */ | |
514 init_completion (&dev->write_finished); | |
515 atomic_set (&dev->write_busy, 1); | |
516 retval = usb_submit_urb(dev->write_urb, GFP_KERNEL); | |
517 if (retval) { | |
518 atomic_set (&dev->write_busy, 0); | |
519 err("%s - failed submitting write urb, error %d", | |
520 __FUNCTION__, retval); | |
521 } else { | |
522 retval = bytes_written; | |
523 } | |
524 | |
525 exit: | |
526 /* unlock the device */ | |
527 up (&dev->sem); | |
528 | |
529 return retval; | |
530 } | |
531 | |
532 | |
533 /** | |
534 */ | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
535 static int dmx_usb_ioctl (struct file *file, unsigned int cmd, unsigned long arg) |
355 | 536 { |
537 struct dmx_usb_device *dev; | |
538 | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
539 lock_kernel(); // this was originally getting the Big Kernel |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
540 // Lock. I don't know if that was needed, but I don't want to |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
541 // destabilize anything as I move to unlocked_ioctl |
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
542 |
355 | 543 dev = (struct dmx_usb_device *)file->private_data; |
544 | |
545 /* lock this object */ | |
546 down (&dev->sem); | |
547 | |
548 /* verify that the device wasn't unplugged */ | |
549 if (!dev->present) { | |
550 up (&dev->sem); | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
551 unlock_kernel(); |
355 | 552 return -ENODEV; |
553 } | |
554 | |
555 dbg("%s - minor %d, cmd 0x%.4x, arg %ld", __FUNCTION__, | |
556 dev->minor, cmd, arg); | |
557 | |
558 /* fill in your device specific stuff here */ | |
559 | |
560 /* unlock the device */ | |
561 up (&dev->sem); | |
562 | |
634
7d59926031ba
attempted fixes to dmx_usb kernel module for 2.6.38. untested; may crash you
drewp@bigasterisk.com
parents:
633
diff
changeset
|
563 unlock_kernel(); |
355 | 564 /* return that we did not understand this ioctl call */ |
565 return -ENOTTY; | |
566 } | |
567 | |
568 | |
569 /** | |
570 */ | |
571 static void dmx_usb_write_bulk_callback (struct urb *urb, struct pt_regs *regs) | |
572 { | |
573 struct dmx_usb_device *dev = (struct dmx_usb_device *)urb->context; | |
574 | |
575 dbg("%s - minor %d", __FUNCTION__, dev->minor); | |
576 | |
577 /* sync/async unlink faults aren't errors */ | |
578 if (urb->status && !(urb->status == -ENOENT || | |
579 urb->status == -ECONNRESET)) { | |
580 dbg("%s - nonzero write bulk status received: %d", | |
581 __FUNCTION__, urb->status); | |
582 } | |
583 | |
584 /* notify anyone waiting that the write has finished */ | |
585 atomic_set (&dev->write_busy, 0); | |
586 complete (&dev->write_finished); | |
587 } | |
588 | |
589 /** | |
590 * | |
591 * Called by the usb core when a new device is connected that it thinks | |
592 * this driver might be interested in. | |
593 */ | |
594 static int dmx_usb_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
595 { | |
596 struct usb_device *udev = interface_to_usbdev(interface); | |
597 struct dmx_usb_device *dev = NULL; | |
598 struct usb_host_interface *iface_desc; | |
599 struct usb_endpoint_descriptor *endpoint; | |
600 size_t buffer_size; | |
601 int i; | |
602 int retval = -ENOMEM; | |
603 | |
604 /* See if the device offered us matches what we can accept */ | |
605 if ((udev->descriptor.idVendor != FTDI_VID) || | |
606 (udev->descriptor.idProduct != FTDI_8U232AM_PID)) { | |
607 return -ENODEV; | |
608 } | |
609 | |
610 /* allocate memory for our device state and initialize it */ | |
611 dev = kmalloc (sizeof(struct dmx_usb_device), GFP_KERNEL); | |
612 if (dev == NULL) { | |
613 err ("Out of memory"); | |
614 return -ENOMEM; | |
615 } | |
616 memset (dev, 0x00, sizeof (*dev)); | |
617 | |
618 init_MUTEX (&dev->sem); | |
619 dev->udev = udev; | |
620 dev->interface = interface; | |
621 | |
622 /* set up the endpoint information */ | |
623 /* check out the endpoints */ | |
624 /* use only the first bulk-in and bulk-out endpoints */ | |
625 iface_desc = &interface->altsetting[0]; | |
626 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { | |
627 endpoint = &iface_desc->endpoint[i].desc; | |
628 | |
629 if (!dev->bulk_in_endpointAddr && | |
630 (endpoint->bEndpointAddress & USB_DIR_IN) && | |
631 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
632 == USB_ENDPOINT_XFER_BULK)) { | |
633 /* we found a bulk in endpoint */ | |
634 buffer_size = endpoint->wMaxPacketSize; | |
635 dev->bulk_in_size = buffer_size; | |
636 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress; | |
637 dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL); | |
638 if (!dev->bulk_in_buffer) { | |
639 err("Couldn't allocate bulk_in_buffer"); | |
640 goto error; | |
641 } | |
642 } | |
643 | |
644 if (!dev->bulk_out_endpointAddr && | |
645 !(endpoint->bEndpointAddress & USB_DIR_IN) && | |
646 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) | |
647 == USB_ENDPOINT_XFER_BULK)) { | |
648 /* we found a bulk out endpoint */ | |
649 /* a probe() may sleep and has no restrictions on memory allocations */ | |
650 dev->write_urb = usb_alloc_urb(0, GFP_KERNEL); | |
651 if (!dev->write_urb) { | |
652 err("No free urbs available"); | |
653 goto error; | |
654 } | |
655 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress; | |
656 | |
657 /* on some platforms using this kind of buffer alloc | |
658 * call eliminates a dma "bounce buffer". | |
659 * | |
660 * NOTE: you'd normally want i/o buffers that hold | |
661 * more than one packet, so that i/o delays between | |
662 * packets don't hurt throughput. | |
663 */ | |
664 buffer_size = endpoint->wMaxPacketSize; | |
665 dev->bulk_out_size = 513; | |
666 dev->write_urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; | |
633
3648a427e9a0
switch to usb_free_coherent and usb_alloc_coherent for new linux kernel
Drew Perttula <drewp@bigasterisk.com>
parents:
555
diff
changeset
|
667 dev->bulk_out_buffer = usb_alloc_coherent (udev, |
355 | 668 buffer_size, GFP_KERNEL, |
669 &dev->write_urb->transfer_dma); | |
670 if (!dev->bulk_out_buffer) { | |
671 err("Couldn't allocate bulk_out_buffer"); | |
672 goto error; | |
673 } | |
674 usb_fill_bulk_urb(dev->write_urb, udev, | |
675 usb_sndbulkpipe(udev, | |
676 endpoint->bEndpointAddress), | |
677 dev->bulk_out_buffer, buffer_size, | |
678 dmx_usb_write_bulk_callback, dev); | |
679 } | |
680 } | |
681 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) { | |
682 err("Couldn't find both bulk-in and bulk-out endpoints"); | |
683 goto error; | |
684 } | |
685 | |
686 dmx_usb_setup(dev); | |
687 | |
688 /* allow device read, write and ioctl */ | |
689 dev->present = 1; | |
690 | |
691 /* we can register the device now, as it is ready */ | |
692 usb_set_intfdata (interface, dev); | |
693 retval = usb_register_dev (interface, &dmx_usb_class); | |
694 if (retval) { | |
695 /* something prevented us from registering this driver */ | |
696 err ("Not able to get a minor for this device."); | |
697 usb_set_intfdata (interface, NULL); | |
698 goto error; | |
699 } | |
700 | |
701 dev->minor = interface->minor; | |
702 | |
703 /* let the user know what node this device is now attached to */ | |
555
1b56c80b1ee4
patch for newer linux kernels which apparently have err() but not info()
drewp@bigasterisk.com
parents:
355
diff
changeset
|
704 err ("info: DMX USB device now attached to dmx%d", dev->minor); |
355 | 705 return 0; |
706 | |
707 error: | |
708 dmx_usb_delete (dev); | |
709 return retval; | |
710 } | |
711 | |
712 | |
713 /** | |
714 * | |
715 * Called by the usb core when the device is removed from the system. | |
716 * | |
717 * This routine guarantees that the driver will not submit any more urbs | |
718 * by clearing dev->udev. It is also supposed to terminate any currently | |
719 * active urbs. Unfortunately, usb_bulk_msg(), used in dmx_usb_read(), does | |
720 * not provide any way to do this. But at least we can cancel an active | |
721 * write. | |
722 */ | |
723 static void dmx_usb_disconnect(struct usb_interface *interface) | |
724 { | |
725 struct dmx_usb_device *dev; | |
726 int minor; | |
727 | |
728 /* prevent races with open() */ | |
729 down (&disconnect_sem); | |
730 | |
731 dev = usb_get_intfdata (interface); | |
732 usb_set_intfdata (interface, NULL); | |
733 | |
734 down (&dev->sem); | |
735 | |
736 minor = dev->minor; | |
737 | |
738 /* give back our minor */ | |
739 usb_deregister_dev (interface, &dmx_usb_class); | |
740 | |
741 /* terminate an ongoing write */ | |
742 if (atomic_read (&dev->write_busy)) { | |
743 usb_unlink_urb (dev->write_urb); | |
744 wait_for_completion (&dev->write_finished); | |
745 } | |
746 | |
747 /* prevent device read, write and ioctl */ | |
748 dev->present = 0; | |
749 | |
750 up (&dev->sem); | |
751 | |
752 /* if the device is opened, dmx_usb_release will clean this up */ | |
753 if (!dev->open) | |
754 dmx_usb_delete (dev); | |
755 | |
756 up (&disconnect_sem); | |
757 | |
555
1b56c80b1ee4
patch for newer linux kernels which apparently have err() but not info()
drewp@bigasterisk.com
parents:
355
diff
changeset
|
758 err("info: DMX USB #%d now disconnected", minor); |
355 | 759 } |
760 | |
761 | |
762 | |
763 /** | |
764 * dmx_usb_init | |
765 */ | |
766 static int __init dmx_usb_init(void) | |
767 { | |
768 int result; | |
769 | |
770 /* register this driver with the USB subsystem */ | |
771 result = usb_register(&dmx_usb_driver); | |
772 if (result) { | |
773 err("usb_register failed. Error number %d", | |
774 result); | |
775 return result; | |
776 } | |
777 | |
555
1b56c80b1ee4
patch for newer linux kernels which apparently have err() but not info()
drewp@bigasterisk.com
parents:
355
diff
changeset
|
778 err("info: " DRIVER_DESC " " DRIVER_VERSION); |
355 | 779 return 0; |
780 } | |
781 | |
782 | |
783 /** | |
784 * dmx_usb_exit | |
785 */ | |
786 static void __exit dmx_usb_exit(void) | |
787 { | |
788 /* deregister this driver with the USB subsystem */ | |
789 usb_deregister(&dmx_usb_driver); | |
790 } | |
791 | |
792 | |
793 module_init (dmx_usb_init); | |
794 module_exit (dmx_usb_exit); | |
795 | |
796 MODULE_AUTHOR(DRIVER_AUTHOR); | |
797 MODULE_DESCRIPTION(DRIVER_DESC); | |
798 MODULE_LICENSE("GPL"); | |
799 |