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