1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Native support for the I/O-Warrior USB devices
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (c) 2003-2005, 2020 Code Mercenaries GmbH
6*4882a593Smuzhiyun * written by Christian Lucht <lucht@codemercs.com> and
7*4882a593Smuzhiyun * Christoph Jung <jung@codemercs.com>
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * based on
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun * usb-skeleton.c by Greg Kroah-Hartman <greg@kroah.com>
12*4882a593Smuzhiyun * brlvger.c by Stephane Dalton <sdalton@videotron.ca>
13*4882a593Smuzhiyun * and Stephane Doyon <s.doyon@videotron.ca>
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Released under the GPLv2.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/usb.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/sched.h>
22*4882a593Smuzhiyun #include <linux/mutex.h>
23*4882a593Smuzhiyun #include <linux/poll.h>
24*4882a593Smuzhiyun #include <linux/usb/iowarrior.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
27*4882a593Smuzhiyun #define DRIVER_DESC "USB IO-Warrior driver"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define USB_VENDOR_ID_CODEMERCS 1984
30*4882a593Smuzhiyun /* low speed iowarrior */
31*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW40 0x1500
32*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW24 0x1501
33*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOWPV1 0x1511
34*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOWPV2 0x1512
35*4882a593Smuzhiyun /* full speed iowarrior */
36*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW56 0x1503
37*4882a593Smuzhiyun /* fuller speed iowarrior */
38*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW28 0x1504
39*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW28L 0x1505
40*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW100 0x1506
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* OEMed devices */
43*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW24SAG 0x158a
44*4882a593Smuzhiyun #define USB_DEVICE_ID_CODEMERCS_IOW56AM 0x158b
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun /* Get a minor range for your devices from the usb maintainer */
47*4882a593Smuzhiyun #ifdef CONFIG_USB_DYNAMIC_MINORS
48*4882a593Smuzhiyun #define IOWARRIOR_MINOR_BASE 0
49*4882a593Smuzhiyun #else
50*4882a593Smuzhiyun #define IOWARRIOR_MINOR_BASE 208 // SKELETON_MINOR_BASE 192 + 16, not official yet
51*4882a593Smuzhiyun #endif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* interrupt input queue size */
54*4882a593Smuzhiyun #define MAX_INTERRUPT_BUFFER 16
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun maximum number of urbs that are submitted for writes at the same time,
57*4882a593Smuzhiyun this applies to the IOWarrior56 only!
58*4882a593Smuzhiyun IOWarrior24 and IOWarrior40 use synchronous usb_control_msg calls.
59*4882a593Smuzhiyun */
60*4882a593Smuzhiyun #define MAX_WRITES_IN_FLIGHT 4
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun MODULE_AUTHOR(DRIVER_AUTHOR);
63*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
64*4882a593Smuzhiyun MODULE_LICENSE("GPL");
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static struct usb_driver iowarrior_driver;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /*--------------*/
69*4882a593Smuzhiyun /* data */
70*4882a593Smuzhiyun /*--------------*/
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun /* Structure to hold all of our device specific stuff */
73*4882a593Smuzhiyun struct iowarrior {
74*4882a593Smuzhiyun struct mutex mutex; /* locks this structure */
75*4882a593Smuzhiyun struct usb_device *udev; /* save off the usb device pointer */
76*4882a593Smuzhiyun struct usb_interface *interface; /* the interface for this device */
77*4882a593Smuzhiyun unsigned char minor; /* the starting minor number for this device */
78*4882a593Smuzhiyun struct usb_endpoint_descriptor *int_out_endpoint; /* endpoint for reading (needed for IOW56 only) */
79*4882a593Smuzhiyun struct usb_endpoint_descriptor *int_in_endpoint; /* endpoint for reading */
80*4882a593Smuzhiyun struct urb *int_in_urb; /* the urb for reading data */
81*4882a593Smuzhiyun unsigned char *int_in_buffer; /* buffer for data to be read */
82*4882a593Smuzhiyun unsigned char serial_number; /* to detect lost packages */
83*4882a593Smuzhiyun unsigned char *read_queue; /* size is MAX_INTERRUPT_BUFFER * packet size */
84*4882a593Smuzhiyun wait_queue_head_t read_wait;
85*4882a593Smuzhiyun wait_queue_head_t write_wait; /* wait-queue for writing to the device */
86*4882a593Smuzhiyun atomic_t write_busy; /* number of write-urbs submitted */
87*4882a593Smuzhiyun atomic_t read_idx;
88*4882a593Smuzhiyun atomic_t intr_idx;
89*4882a593Smuzhiyun atomic_t overflow_flag; /* signals an index 'rollover' */
90*4882a593Smuzhiyun int present; /* this is 1 as long as the device is connected */
91*4882a593Smuzhiyun int opened; /* this is 1 if the device is currently open */
92*4882a593Smuzhiyun char chip_serial[9]; /* the serial number string of the chip connected */
93*4882a593Smuzhiyun int report_size; /* number of bytes in a report */
94*4882a593Smuzhiyun u16 product_id;
95*4882a593Smuzhiyun struct usb_anchor submitted;
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /*--------------*/
99*4882a593Smuzhiyun /* globals */
100*4882a593Smuzhiyun /*--------------*/
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #define USB_REQ_GET_REPORT 0x01
103*4882a593Smuzhiyun //#if 0
usb_get_report(struct usb_device * dev,struct usb_host_interface * inter,unsigned char type,unsigned char id,void * buf,int size)104*4882a593Smuzhiyun static int usb_get_report(struct usb_device *dev,
105*4882a593Smuzhiyun struct usb_host_interface *inter, unsigned char type,
106*4882a593Smuzhiyun unsigned char id, void *buf, int size)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
109*4882a593Smuzhiyun USB_REQ_GET_REPORT,
110*4882a593Smuzhiyun USB_DIR_IN | USB_TYPE_CLASS |
111*4882a593Smuzhiyun USB_RECIP_INTERFACE, (type << 8) + id,
112*4882a593Smuzhiyun inter->desc.bInterfaceNumber, buf, size,
113*4882a593Smuzhiyun USB_CTRL_GET_TIMEOUT);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun //#endif
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun #define USB_REQ_SET_REPORT 0x09
118*4882a593Smuzhiyun
usb_set_report(struct usb_interface * intf,unsigned char type,unsigned char id,void * buf,int size)119*4882a593Smuzhiyun static int usb_set_report(struct usb_interface *intf, unsigned char type,
120*4882a593Smuzhiyun unsigned char id, void *buf, int size)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun return usb_control_msg(interface_to_usbdev(intf),
123*4882a593Smuzhiyun usb_sndctrlpipe(interface_to_usbdev(intf), 0),
124*4882a593Smuzhiyun USB_REQ_SET_REPORT,
125*4882a593Smuzhiyun USB_TYPE_CLASS | USB_RECIP_INTERFACE,
126*4882a593Smuzhiyun (type << 8) + id,
127*4882a593Smuzhiyun intf->cur_altsetting->desc.bInterfaceNumber, buf,
128*4882a593Smuzhiyun size, 1000);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*---------------------*/
132*4882a593Smuzhiyun /* driver registration */
133*4882a593Smuzhiyun /*---------------------*/
134*4882a593Smuzhiyun /* table of devices that work with this driver */
135*4882a593Smuzhiyun static const struct usb_device_id iowarrior_ids[] = {
136*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)},
137*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)},
138*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)},
139*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)},
140*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)},
141*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24SAG)},
142*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56AM)},
143*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28)},
144*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28L)},
145*4882a593Smuzhiyun {USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW100)},
146*4882a593Smuzhiyun {} /* Terminating entry */
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, iowarrior_ids);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun * USB callback handler for reading data
152*4882a593Smuzhiyun */
iowarrior_callback(struct urb * urb)153*4882a593Smuzhiyun static void iowarrior_callback(struct urb *urb)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun struct iowarrior *dev = urb->context;
156*4882a593Smuzhiyun int intr_idx;
157*4882a593Smuzhiyun int read_idx;
158*4882a593Smuzhiyun int aux_idx;
159*4882a593Smuzhiyun int offset;
160*4882a593Smuzhiyun int status = urb->status;
161*4882a593Smuzhiyun int retval;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun switch (status) {
164*4882a593Smuzhiyun case 0:
165*4882a593Smuzhiyun /* success */
166*4882a593Smuzhiyun break;
167*4882a593Smuzhiyun case -ECONNRESET:
168*4882a593Smuzhiyun case -ENOENT:
169*4882a593Smuzhiyun case -ESHUTDOWN:
170*4882a593Smuzhiyun return;
171*4882a593Smuzhiyun default:
172*4882a593Smuzhiyun goto exit;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun intr_idx = atomic_read(&dev->intr_idx);
176*4882a593Smuzhiyun /* aux_idx become previous intr_idx */
177*4882a593Smuzhiyun aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
178*4882a593Smuzhiyun read_idx = atomic_read(&dev->read_idx);
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* queue is not empty and it's interface 0 */
181*4882a593Smuzhiyun if ((intr_idx != read_idx)
182*4882a593Smuzhiyun && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
183*4882a593Smuzhiyun /* + 1 for serial number */
184*4882a593Smuzhiyun offset = aux_idx * (dev->report_size + 1);
185*4882a593Smuzhiyun if (!memcmp
186*4882a593Smuzhiyun (dev->read_queue + offset, urb->transfer_buffer,
187*4882a593Smuzhiyun dev->report_size)) {
188*4882a593Smuzhiyun /* equal values on interface 0 will be ignored */
189*4882a593Smuzhiyun goto exit;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /* aux_idx become next intr_idx */
194*4882a593Smuzhiyun aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
195*4882a593Smuzhiyun if (read_idx == aux_idx) {
196*4882a593Smuzhiyun /* queue full, dropping oldest input */
197*4882a593Smuzhiyun read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
198*4882a593Smuzhiyun atomic_set(&dev->read_idx, read_idx);
199*4882a593Smuzhiyun atomic_set(&dev->overflow_flag, 1);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* +1 for serial number */
203*4882a593Smuzhiyun offset = intr_idx * (dev->report_size + 1);
204*4882a593Smuzhiyun memcpy(dev->read_queue + offset, urb->transfer_buffer,
205*4882a593Smuzhiyun dev->report_size);
206*4882a593Smuzhiyun *(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun atomic_set(&dev->intr_idx, aux_idx);
209*4882a593Smuzhiyun /* tell the blocking read about the new data */
210*4882a593Smuzhiyun wake_up_interruptible(&dev->read_wait);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun exit:
213*4882a593Smuzhiyun retval = usb_submit_urb(urb, GFP_ATOMIC);
214*4882a593Smuzhiyun if (retval)
215*4882a593Smuzhiyun dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
216*4882a593Smuzhiyun __func__, retval);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun * USB Callback handler for write-ops
222*4882a593Smuzhiyun */
iowarrior_write_callback(struct urb * urb)223*4882a593Smuzhiyun static void iowarrior_write_callback(struct urb *urb)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct iowarrior *dev;
226*4882a593Smuzhiyun int status = urb->status;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun dev = urb->context;
229*4882a593Smuzhiyun /* sync/async unlink faults aren't errors */
230*4882a593Smuzhiyun if (status &&
231*4882a593Smuzhiyun !(status == -ENOENT ||
232*4882a593Smuzhiyun status == -ECONNRESET || status == -ESHUTDOWN)) {
233*4882a593Smuzhiyun dev_dbg(&dev->interface->dev,
234*4882a593Smuzhiyun "nonzero write bulk status received: %d\n", status);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun /* free up our allocated buffer */
237*4882a593Smuzhiyun usb_free_coherent(urb->dev, urb->transfer_buffer_length,
238*4882a593Smuzhiyun urb->transfer_buffer, urb->transfer_dma);
239*4882a593Smuzhiyun /* tell a waiting writer the interrupt-out-pipe is available again */
240*4882a593Smuzhiyun atomic_dec(&dev->write_busy);
241*4882a593Smuzhiyun wake_up_interruptible(&dev->write_wait);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /*
245*4882a593Smuzhiyun * iowarrior_delete
246*4882a593Smuzhiyun */
iowarrior_delete(struct iowarrior * dev)247*4882a593Smuzhiyun static inline void iowarrior_delete(struct iowarrior *dev)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
250*4882a593Smuzhiyun kfree(dev->int_in_buffer);
251*4882a593Smuzhiyun usb_free_urb(dev->int_in_urb);
252*4882a593Smuzhiyun kfree(dev->read_queue);
253*4882a593Smuzhiyun usb_put_intf(dev->interface);
254*4882a593Smuzhiyun kfree(dev);
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /*---------------------*/
258*4882a593Smuzhiyun /* fops implementation */
259*4882a593Smuzhiyun /*---------------------*/
260*4882a593Smuzhiyun
read_index(struct iowarrior * dev)261*4882a593Smuzhiyun static int read_index(struct iowarrior *dev)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun int intr_idx, read_idx;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun read_idx = atomic_read(&dev->read_idx);
266*4882a593Smuzhiyun intr_idx = atomic_read(&dev->intr_idx);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun return (read_idx == intr_idx ? -1 : read_idx);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /*
272*4882a593Smuzhiyun * iowarrior_read
273*4882a593Smuzhiyun */
iowarrior_read(struct file * file,char __user * buffer,size_t count,loff_t * ppos)274*4882a593Smuzhiyun static ssize_t iowarrior_read(struct file *file, char __user *buffer,
275*4882a593Smuzhiyun size_t count, loff_t *ppos)
276*4882a593Smuzhiyun {
277*4882a593Smuzhiyun struct iowarrior *dev;
278*4882a593Smuzhiyun int read_idx;
279*4882a593Smuzhiyun int offset;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun dev = file->private_data;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun /* verify that the device wasn't unplugged */
284*4882a593Smuzhiyun if (!dev || !dev->present)
285*4882a593Smuzhiyun return -ENODEV;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
288*4882a593Smuzhiyun dev->minor, count);
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun /* read count must be packet size (+ time stamp) */
291*4882a593Smuzhiyun if ((count != dev->report_size)
292*4882a593Smuzhiyun && (count != (dev->report_size + 1)))
293*4882a593Smuzhiyun return -EINVAL;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* repeat until no buffer overrun in callback handler occur */
296*4882a593Smuzhiyun do {
297*4882a593Smuzhiyun atomic_set(&dev->overflow_flag, 0);
298*4882a593Smuzhiyun if ((read_idx = read_index(dev)) == -1) {
299*4882a593Smuzhiyun /* queue empty */
300*4882a593Smuzhiyun if (file->f_flags & O_NONBLOCK)
301*4882a593Smuzhiyun return -EAGAIN;
302*4882a593Smuzhiyun else {
303*4882a593Smuzhiyun //next line will return when there is either new data, or the device is unplugged
304*4882a593Smuzhiyun int r = wait_event_interruptible(dev->read_wait,
305*4882a593Smuzhiyun (!dev->present
306*4882a593Smuzhiyun || (read_idx =
307*4882a593Smuzhiyun read_index
308*4882a593Smuzhiyun (dev)) !=
309*4882a593Smuzhiyun -1));
310*4882a593Smuzhiyun if (r) {
311*4882a593Smuzhiyun //we were interrupted by a signal
312*4882a593Smuzhiyun return -ERESTART;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun if (!dev->present) {
315*4882a593Smuzhiyun //The device was unplugged
316*4882a593Smuzhiyun return -ENODEV;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun if (read_idx == -1) {
319*4882a593Smuzhiyun // Can this happen ???
320*4882a593Smuzhiyun return 0;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun offset = read_idx * (dev->report_size + 1);
326*4882a593Smuzhiyun if (copy_to_user(buffer, dev->read_queue + offset, count)) {
327*4882a593Smuzhiyun return -EFAULT;
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun } while (atomic_read(&dev->overflow_flag));
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
332*4882a593Smuzhiyun atomic_set(&dev->read_idx, read_idx);
333*4882a593Smuzhiyun return count;
334*4882a593Smuzhiyun }
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /*
337*4882a593Smuzhiyun * iowarrior_write
338*4882a593Smuzhiyun */
iowarrior_write(struct file * file,const char __user * user_buffer,size_t count,loff_t * ppos)339*4882a593Smuzhiyun static ssize_t iowarrior_write(struct file *file,
340*4882a593Smuzhiyun const char __user *user_buffer,
341*4882a593Smuzhiyun size_t count, loff_t *ppos)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun struct iowarrior *dev;
344*4882a593Smuzhiyun int retval = 0;
345*4882a593Smuzhiyun char *buf = NULL; /* for IOW24 and IOW56 we need a buffer */
346*4882a593Smuzhiyun struct urb *int_out_urb = NULL;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun dev = file->private_data;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun mutex_lock(&dev->mutex);
351*4882a593Smuzhiyun /* verify that the device wasn't unplugged */
352*4882a593Smuzhiyun if (!dev->present) {
353*4882a593Smuzhiyun retval = -ENODEV;
354*4882a593Smuzhiyun goto exit;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun dev_dbg(&dev->interface->dev, "minor %d, count = %zd\n",
357*4882a593Smuzhiyun dev->minor, count);
358*4882a593Smuzhiyun /* if count is 0 we're already done */
359*4882a593Smuzhiyun if (count == 0) {
360*4882a593Smuzhiyun retval = 0;
361*4882a593Smuzhiyun goto exit;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun /* We only accept full reports */
364*4882a593Smuzhiyun if (count != dev->report_size) {
365*4882a593Smuzhiyun retval = -EINVAL;
366*4882a593Smuzhiyun goto exit;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun switch (dev->product_id) {
369*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW24:
370*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW24SAG:
371*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOWPV1:
372*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOWPV2:
373*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW40:
374*4882a593Smuzhiyun /* IOW24 and IOW40 use a synchronous call */
375*4882a593Smuzhiyun buf = memdup_user(user_buffer, count);
376*4882a593Smuzhiyun if (IS_ERR(buf)) {
377*4882a593Smuzhiyun retval = PTR_ERR(buf);
378*4882a593Smuzhiyun goto exit;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun retval = usb_set_report(dev->interface, 2, 0, buf, count);
381*4882a593Smuzhiyun kfree(buf);
382*4882a593Smuzhiyun goto exit;
383*4882a593Smuzhiyun break;
384*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW56:
385*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW56AM:
386*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW28:
387*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW28L:
388*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW100:
389*4882a593Smuzhiyun /* The IOW56 uses asynchronous IO and more urbs */
390*4882a593Smuzhiyun if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
391*4882a593Smuzhiyun /* Wait until we are below the limit for submitted urbs */
392*4882a593Smuzhiyun if (file->f_flags & O_NONBLOCK) {
393*4882a593Smuzhiyun retval = -EAGAIN;
394*4882a593Smuzhiyun goto exit;
395*4882a593Smuzhiyun } else {
396*4882a593Smuzhiyun retval = wait_event_interruptible(dev->write_wait,
397*4882a593Smuzhiyun (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
398*4882a593Smuzhiyun if (retval) {
399*4882a593Smuzhiyun /* we were interrupted by a signal */
400*4882a593Smuzhiyun retval = -ERESTART;
401*4882a593Smuzhiyun goto exit;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun if (!dev->present) {
404*4882a593Smuzhiyun /* The device was unplugged */
405*4882a593Smuzhiyun retval = -ENODEV;
406*4882a593Smuzhiyun goto exit;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun if (!dev->opened) {
409*4882a593Smuzhiyun /* We were closed while waiting for an URB */
410*4882a593Smuzhiyun retval = -ENODEV;
411*4882a593Smuzhiyun goto exit;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun }
415*4882a593Smuzhiyun atomic_inc(&dev->write_busy);
416*4882a593Smuzhiyun int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
417*4882a593Smuzhiyun if (!int_out_urb) {
418*4882a593Smuzhiyun retval = -ENOMEM;
419*4882a593Smuzhiyun goto error_no_urb;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun buf = usb_alloc_coherent(dev->udev, dev->report_size,
422*4882a593Smuzhiyun GFP_KERNEL, &int_out_urb->transfer_dma);
423*4882a593Smuzhiyun if (!buf) {
424*4882a593Smuzhiyun retval = -ENOMEM;
425*4882a593Smuzhiyun dev_dbg(&dev->interface->dev,
426*4882a593Smuzhiyun "Unable to allocate buffer\n");
427*4882a593Smuzhiyun goto error_no_buffer;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun usb_fill_int_urb(int_out_urb, dev->udev,
430*4882a593Smuzhiyun usb_sndintpipe(dev->udev,
431*4882a593Smuzhiyun dev->int_out_endpoint->bEndpointAddress),
432*4882a593Smuzhiyun buf, dev->report_size,
433*4882a593Smuzhiyun iowarrior_write_callback, dev,
434*4882a593Smuzhiyun dev->int_out_endpoint->bInterval);
435*4882a593Smuzhiyun int_out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
436*4882a593Smuzhiyun if (copy_from_user(buf, user_buffer, count)) {
437*4882a593Smuzhiyun retval = -EFAULT;
438*4882a593Smuzhiyun goto error;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun usb_anchor_urb(int_out_urb, &dev->submitted);
441*4882a593Smuzhiyun retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
442*4882a593Smuzhiyun if (retval) {
443*4882a593Smuzhiyun dev_dbg(&dev->interface->dev,
444*4882a593Smuzhiyun "submit error %d for urb nr.%d\n",
445*4882a593Smuzhiyun retval, atomic_read(&dev->write_busy));
446*4882a593Smuzhiyun usb_unanchor_urb(int_out_urb);
447*4882a593Smuzhiyun goto error;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun /* submit was ok */
450*4882a593Smuzhiyun retval = count;
451*4882a593Smuzhiyun usb_free_urb(int_out_urb);
452*4882a593Smuzhiyun goto exit;
453*4882a593Smuzhiyun break;
454*4882a593Smuzhiyun default:
455*4882a593Smuzhiyun /* what do we have here ? An unsupported Product-ID ? */
456*4882a593Smuzhiyun dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n",
457*4882a593Smuzhiyun __func__, dev->product_id);
458*4882a593Smuzhiyun retval = -EFAULT;
459*4882a593Smuzhiyun goto exit;
460*4882a593Smuzhiyun break;
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun error:
463*4882a593Smuzhiyun usb_free_coherent(dev->udev, dev->report_size, buf,
464*4882a593Smuzhiyun int_out_urb->transfer_dma);
465*4882a593Smuzhiyun error_no_buffer:
466*4882a593Smuzhiyun usb_free_urb(int_out_urb);
467*4882a593Smuzhiyun error_no_urb:
468*4882a593Smuzhiyun atomic_dec(&dev->write_busy);
469*4882a593Smuzhiyun wake_up_interruptible(&dev->write_wait);
470*4882a593Smuzhiyun exit:
471*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
472*4882a593Smuzhiyun return retval;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun * iowarrior_ioctl
477*4882a593Smuzhiyun */
iowarrior_ioctl(struct file * file,unsigned int cmd,unsigned long arg)478*4882a593Smuzhiyun static long iowarrior_ioctl(struct file *file, unsigned int cmd,
479*4882a593Smuzhiyun unsigned long arg)
480*4882a593Smuzhiyun {
481*4882a593Smuzhiyun struct iowarrior *dev = NULL;
482*4882a593Smuzhiyun __u8 *buffer;
483*4882a593Smuzhiyun __u8 __user *user_buffer;
484*4882a593Smuzhiyun int retval;
485*4882a593Smuzhiyun int io_res; /* checks for bytes read/written and copy_to/from_user results */
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun dev = file->private_data;
488*4882a593Smuzhiyun if (!dev)
489*4882a593Smuzhiyun return -ENODEV;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun buffer = kzalloc(dev->report_size, GFP_KERNEL);
492*4882a593Smuzhiyun if (!buffer)
493*4882a593Smuzhiyun return -ENOMEM;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun mutex_lock(&dev->mutex);
496*4882a593Smuzhiyun
497*4882a593Smuzhiyun /* verify that the device wasn't unplugged */
498*4882a593Smuzhiyun if (!dev->present) {
499*4882a593Smuzhiyun retval = -ENODEV;
500*4882a593Smuzhiyun goto error_out;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun dev_dbg(&dev->interface->dev, "minor %d, cmd 0x%.4x, arg %ld\n",
504*4882a593Smuzhiyun dev->minor, cmd, arg);
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun retval = 0;
507*4882a593Smuzhiyun io_res = 0;
508*4882a593Smuzhiyun switch (cmd) {
509*4882a593Smuzhiyun case IOW_WRITE:
510*4882a593Smuzhiyun if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 ||
511*4882a593Smuzhiyun dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24SAG ||
512*4882a593Smuzhiyun dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 ||
513*4882a593Smuzhiyun dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 ||
514*4882a593Smuzhiyun dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) {
515*4882a593Smuzhiyun user_buffer = (__u8 __user *)arg;
516*4882a593Smuzhiyun io_res = copy_from_user(buffer, user_buffer,
517*4882a593Smuzhiyun dev->report_size);
518*4882a593Smuzhiyun if (io_res) {
519*4882a593Smuzhiyun retval = -EFAULT;
520*4882a593Smuzhiyun } else {
521*4882a593Smuzhiyun io_res = usb_set_report(dev->interface, 2, 0,
522*4882a593Smuzhiyun buffer,
523*4882a593Smuzhiyun dev->report_size);
524*4882a593Smuzhiyun if (io_res < 0)
525*4882a593Smuzhiyun retval = io_res;
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun } else {
528*4882a593Smuzhiyun retval = -EINVAL;
529*4882a593Smuzhiyun dev_err(&dev->interface->dev,
530*4882a593Smuzhiyun "ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
531*4882a593Smuzhiyun dev->product_id);
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun break;
534*4882a593Smuzhiyun case IOW_READ:
535*4882a593Smuzhiyun user_buffer = (__u8 __user *)arg;
536*4882a593Smuzhiyun io_res = usb_get_report(dev->udev,
537*4882a593Smuzhiyun dev->interface->cur_altsetting, 1, 0,
538*4882a593Smuzhiyun buffer, dev->report_size);
539*4882a593Smuzhiyun if (io_res < 0)
540*4882a593Smuzhiyun retval = io_res;
541*4882a593Smuzhiyun else {
542*4882a593Smuzhiyun io_res = copy_to_user(user_buffer, buffer, dev->report_size);
543*4882a593Smuzhiyun if (io_res)
544*4882a593Smuzhiyun retval = -EFAULT;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun break;
547*4882a593Smuzhiyun case IOW_GETINFO:
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun /* Report available information for the device */
550*4882a593Smuzhiyun struct iowarrior_info info;
551*4882a593Smuzhiyun /* needed for power consumption */
552*4882a593Smuzhiyun struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun memset(&info, 0, sizeof(info));
555*4882a593Smuzhiyun /* directly from the descriptor */
556*4882a593Smuzhiyun info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
557*4882a593Smuzhiyun info.product = dev->product_id;
558*4882a593Smuzhiyun info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice);
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun /* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
561*4882a593Smuzhiyun info.speed = dev->udev->speed;
562*4882a593Smuzhiyun info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
563*4882a593Smuzhiyun info.report_size = dev->report_size;
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun /* serial number string has been read earlier 8 chars or empty string */
566*4882a593Smuzhiyun memcpy(info.serial, dev->chip_serial,
567*4882a593Smuzhiyun sizeof(dev->chip_serial));
568*4882a593Smuzhiyun if (cfg_descriptor == NULL) {
569*4882a593Smuzhiyun info.power = -1; /* no information available */
570*4882a593Smuzhiyun } else {
571*4882a593Smuzhiyun /* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
572*4882a593Smuzhiyun info.power = cfg_descriptor->bMaxPower * 2;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
575*4882a593Smuzhiyun sizeof(struct iowarrior_info));
576*4882a593Smuzhiyun if (io_res)
577*4882a593Smuzhiyun retval = -EFAULT;
578*4882a593Smuzhiyun break;
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun default:
581*4882a593Smuzhiyun /* return that we did not understand this ioctl call */
582*4882a593Smuzhiyun retval = -ENOTTY;
583*4882a593Smuzhiyun break;
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun error_out:
586*4882a593Smuzhiyun /* unlock the device */
587*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
588*4882a593Smuzhiyun kfree(buffer);
589*4882a593Smuzhiyun return retval;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /*
593*4882a593Smuzhiyun * iowarrior_open
594*4882a593Smuzhiyun */
iowarrior_open(struct inode * inode,struct file * file)595*4882a593Smuzhiyun static int iowarrior_open(struct inode *inode, struct file *file)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun struct iowarrior *dev = NULL;
598*4882a593Smuzhiyun struct usb_interface *interface;
599*4882a593Smuzhiyun int subminor;
600*4882a593Smuzhiyun int retval = 0;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun subminor = iminor(inode);
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun interface = usb_find_interface(&iowarrior_driver, subminor);
605*4882a593Smuzhiyun if (!interface) {
606*4882a593Smuzhiyun pr_err("%s - error, can't find device for minor %d\n",
607*4882a593Smuzhiyun __func__, subminor);
608*4882a593Smuzhiyun return -ENODEV;
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun dev = usb_get_intfdata(interface);
612*4882a593Smuzhiyun if (!dev)
613*4882a593Smuzhiyun return -ENODEV;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun mutex_lock(&dev->mutex);
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /* Only one process can open each device, no sharing. */
618*4882a593Smuzhiyun if (dev->opened) {
619*4882a593Smuzhiyun retval = -EBUSY;
620*4882a593Smuzhiyun goto out;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun /* setup interrupt handler for receiving values */
624*4882a593Smuzhiyun if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) {
625*4882a593Smuzhiyun dev_err(&interface->dev, "Error %d while submitting URB\n", retval);
626*4882a593Smuzhiyun retval = -EFAULT;
627*4882a593Smuzhiyun goto out;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun /* increment our usage count for the driver */
630*4882a593Smuzhiyun ++dev->opened;
631*4882a593Smuzhiyun /* save our object in the file's private structure */
632*4882a593Smuzhiyun file->private_data = dev;
633*4882a593Smuzhiyun retval = 0;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun out:
636*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
637*4882a593Smuzhiyun return retval;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /*
641*4882a593Smuzhiyun * iowarrior_release
642*4882a593Smuzhiyun */
iowarrior_release(struct inode * inode,struct file * file)643*4882a593Smuzhiyun static int iowarrior_release(struct inode *inode, struct file *file)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun struct iowarrior *dev;
646*4882a593Smuzhiyun int retval = 0;
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun dev = file->private_data;
649*4882a593Smuzhiyun if (!dev)
650*4882a593Smuzhiyun return -ENODEV;
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun dev_dbg(&dev->interface->dev, "minor %d\n", dev->minor);
653*4882a593Smuzhiyun
654*4882a593Smuzhiyun /* lock our device */
655*4882a593Smuzhiyun mutex_lock(&dev->mutex);
656*4882a593Smuzhiyun
657*4882a593Smuzhiyun if (dev->opened <= 0) {
658*4882a593Smuzhiyun retval = -ENODEV; /* close called more than once */
659*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
660*4882a593Smuzhiyun } else {
661*4882a593Smuzhiyun dev->opened = 0; /* we're closing now */
662*4882a593Smuzhiyun retval = 0;
663*4882a593Smuzhiyun if (dev->present) {
664*4882a593Smuzhiyun /*
665*4882a593Smuzhiyun The device is still connected so we only shutdown
666*4882a593Smuzhiyun pending read-/write-ops.
667*4882a593Smuzhiyun */
668*4882a593Smuzhiyun usb_kill_urb(dev->int_in_urb);
669*4882a593Smuzhiyun wake_up_interruptible(&dev->read_wait);
670*4882a593Smuzhiyun wake_up_interruptible(&dev->write_wait);
671*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
672*4882a593Smuzhiyun } else {
673*4882a593Smuzhiyun /* The device was unplugged, cleanup resources */
674*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
675*4882a593Smuzhiyun iowarrior_delete(dev);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun return retval;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
iowarrior_poll(struct file * file,poll_table * wait)681*4882a593Smuzhiyun static __poll_t iowarrior_poll(struct file *file, poll_table * wait)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun struct iowarrior *dev = file->private_data;
684*4882a593Smuzhiyun __poll_t mask = 0;
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun if (!dev->present)
687*4882a593Smuzhiyun return EPOLLERR | EPOLLHUP;
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun poll_wait(file, &dev->read_wait, wait);
690*4882a593Smuzhiyun poll_wait(file, &dev->write_wait, wait);
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun if (!dev->present)
693*4882a593Smuzhiyun return EPOLLERR | EPOLLHUP;
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun if (read_index(dev) != -1)
696*4882a593Smuzhiyun mask |= EPOLLIN | EPOLLRDNORM;
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
699*4882a593Smuzhiyun mask |= EPOLLOUT | EPOLLWRNORM;
700*4882a593Smuzhiyun return mask;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun
703*4882a593Smuzhiyun /*
704*4882a593Smuzhiyun * File operations needed when we register this driver.
705*4882a593Smuzhiyun * This assumes that this driver NEEDS file operations,
706*4882a593Smuzhiyun * of course, which means that the driver is expected
707*4882a593Smuzhiyun * to have a node in the /dev directory. If the USB
708*4882a593Smuzhiyun * device were for a network interface then the driver
709*4882a593Smuzhiyun * would use "struct net_driver" instead, and a serial
710*4882a593Smuzhiyun * device would use "struct tty_driver".
711*4882a593Smuzhiyun */
712*4882a593Smuzhiyun static const struct file_operations iowarrior_fops = {
713*4882a593Smuzhiyun .owner = THIS_MODULE,
714*4882a593Smuzhiyun .write = iowarrior_write,
715*4882a593Smuzhiyun .read = iowarrior_read,
716*4882a593Smuzhiyun .unlocked_ioctl = iowarrior_ioctl,
717*4882a593Smuzhiyun .open = iowarrior_open,
718*4882a593Smuzhiyun .release = iowarrior_release,
719*4882a593Smuzhiyun .poll = iowarrior_poll,
720*4882a593Smuzhiyun .llseek = noop_llseek,
721*4882a593Smuzhiyun };
722*4882a593Smuzhiyun
iowarrior_devnode(struct device * dev,umode_t * mode)723*4882a593Smuzhiyun static char *iowarrior_devnode(struct device *dev, umode_t *mode)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /*
729*4882a593Smuzhiyun * usb class driver info in order to get a minor number from the usb core,
730*4882a593Smuzhiyun * and to have the device registered with devfs and the driver core
731*4882a593Smuzhiyun */
732*4882a593Smuzhiyun static struct usb_class_driver iowarrior_class = {
733*4882a593Smuzhiyun .name = "iowarrior%d",
734*4882a593Smuzhiyun .devnode = iowarrior_devnode,
735*4882a593Smuzhiyun .fops = &iowarrior_fops,
736*4882a593Smuzhiyun .minor_base = IOWARRIOR_MINOR_BASE,
737*4882a593Smuzhiyun };
738*4882a593Smuzhiyun
739*4882a593Smuzhiyun /*---------------------------------*/
740*4882a593Smuzhiyun /* probe and disconnect functions */
741*4882a593Smuzhiyun /*---------------------------------*/
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun * iowarrior_probe
744*4882a593Smuzhiyun *
745*4882a593Smuzhiyun * Called by the usb core when a new device is connected that it thinks
746*4882a593Smuzhiyun * this driver might be interested in.
747*4882a593Smuzhiyun */
iowarrior_probe(struct usb_interface * interface,const struct usb_device_id * id)748*4882a593Smuzhiyun static int iowarrior_probe(struct usb_interface *interface,
749*4882a593Smuzhiyun const struct usb_device_id *id)
750*4882a593Smuzhiyun {
751*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(interface);
752*4882a593Smuzhiyun struct iowarrior *dev = NULL;
753*4882a593Smuzhiyun struct usb_host_interface *iface_desc;
754*4882a593Smuzhiyun int retval = -ENOMEM;
755*4882a593Smuzhiyun int res;
756*4882a593Smuzhiyun
757*4882a593Smuzhiyun /* allocate memory for our device state and initialize it */
758*4882a593Smuzhiyun dev = kzalloc(sizeof(struct iowarrior), GFP_KERNEL);
759*4882a593Smuzhiyun if (!dev)
760*4882a593Smuzhiyun return retval;
761*4882a593Smuzhiyun
762*4882a593Smuzhiyun mutex_init(&dev->mutex);
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun atomic_set(&dev->intr_idx, 0);
765*4882a593Smuzhiyun atomic_set(&dev->read_idx, 0);
766*4882a593Smuzhiyun atomic_set(&dev->overflow_flag, 0);
767*4882a593Smuzhiyun init_waitqueue_head(&dev->read_wait);
768*4882a593Smuzhiyun atomic_set(&dev->write_busy, 0);
769*4882a593Smuzhiyun init_waitqueue_head(&dev->write_wait);
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun dev->udev = udev;
772*4882a593Smuzhiyun dev->interface = usb_get_intf(interface);
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun iface_desc = interface->cur_altsetting;
775*4882a593Smuzhiyun dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun init_usb_anchor(&dev->submitted);
778*4882a593Smuzhiyun
779*4882a593Smuzhiyun res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
780*4882a593Smuzhiyun if (res) {
781*4882a593Smuzhiyun dev_err(&interface->dev, "no interrupt-in endpoint found\n");
782*4882a593Smuzhiyun retval = res;
783*4882a593Smuzhiyun goto error;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun if ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) ||
787*4882a593Smuzhiyun (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) ||
788*4882a593Smuzhiyun (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) ||
789*4882a593Smuzhiyun (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) ||
790*4882a593Smuzhiyun (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100)) {
791*4882a593Smuzhiyun res = usb_find_last_int_out_endpoint(iface_desc,
792*4882a593Smuzhiyun &dev->int_out_endpoint);
793*4882a593Smuzhiyun if (res) {
794*4882a593Smuzhiyun dev_err(&interface->dev, "no interrupt-out endpoint found\n");
795*4882a593Smuzhiyun retval = res;
796*4882a593Smuzhiyun goto error;
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun }
799*4882a593Smuzhiyun
800*4882a593Smuzhiyun /* we have to check the report_size often, so remember it in the endianness suitable for our machine */
801*4882a593Smuzhiyun dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint);
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun /*
804*4882a593Smuzhiyun * Some devices need the report size to be different than the
805*4882a593Smuzhiyun * endpoint size.
806*4882a593Smuzhiyun */
807*4882a593Smuzhiyun if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
808*4882a593Smuzhiyun switch (dev->product_id) {
809*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW56:
810*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW56AM:
811*4882a593Smuzhiyun dev->report_size = 7;
812*4882a593Smuzhiyun break;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW28:
815*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW28L:
816*4882a593Smuzhiyun dev->report_size = 4;
817*4882a593Smuzhiyun break;
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun case USB_DEVICE_ID_CODEMERCS_IOW100:
820*4882a593Smuzhiyun dev->report_size = 13;
821*4882a593Smuzhiyun break;
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /* create the urb and buffer for reading */
826*4882a593Smuzhiyun dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
827*4882a593Smuzhiyun if (!dev->int_in_urb)
828*4882a593Smuzhiyun goto error;
829*4882a593Smuzhiyun dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
830*4882a593Smuzhiyun if (!dev->int_in_buffer)
831*4882a593Smuzhiyun goto error;
832*4882a593Smuzhiyun usb_fill_int_urb(dev->int_in_urb, dev->udev,
833*4882a593Smuzhiyun usb_rcvintpipe(dev->udev,
834*4882a593Smuzhiyun dev->int_in_endpoint->bEndpointAddress),
835*4882a593Smuzhiyun dev->int_in_buffer, dev->report_size,
836*4882a593Smuzhiyun iowarrior_callback, dev,
837*4882a593Smuzhiyun dev->int_in_endpoint->bInterval);
838*4882a593Smuzhiyun /* create an internal buffer for interrupt data from the device */
839*4882a593Smuzhiyun dev->read_queue =
840*4882a593Smuzhiyun kmalloc_array(dev->report_size + 1, MAX_INTERRUPT_BUFFER,
841*4882a593Smuzhiyun GFP_KERNEL);
842*4882a593Smuzhiyun if (!dev->read_queue)
843*4882a593Smuzhiyun goto error;
844*4882a593Smuzhiyun /* Get the serial-number of the chip */
845*4882a593Smuzhiyun memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
846*4882a593Smuzhiyun usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial,
847*4882a593Smuzhiyun sizeof(dev->chip_serial));
848*4882a593Smuzhiyun if (strlen(dev->chip_serial) != 8)
849*4882a593Smuzhiyun memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
850*4882a593Smuzhiyun
851*4882a593Smuzhiyun /* Set the idle timeout to 0, if this is interface 0 */
852*4882a593Smuzhiyun if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
853*4882a593Smuzhiyun usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
854*4882a593Smuzhiyun 0x0A,
855*4882a593Smuzhiyun USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
856*4882a593Smuzhiyun 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
857*4882a593Smuzhiyun }
858*4882a593Smuzhiyun /* allow device read and ioctl */
859*4882a593Smuzhiyun dev->present = 1;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun /* we can register the device now, as it is ready */
862*4882a593Smuzhiyun usb_set_intfdata(interface, dev);
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun retval = usb_register_dev(interface, &iowarrior_class);
865*4882a593Smuzhiyun if (retval) {
866*4882a593Smuzhiyun /* something prevented us from registering this driver */
867*4882a593Smuzhiyun dev_err(&interface->dev, "Not able to get a minor for this device.\n");
868*4882a593Smuzhiyun goto error;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun dev->minor = interface->minor;
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun /* let the user know what node this device is now attached to */
874*4882a593Smuzhiyun dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d "
875*4882a593Smuzhiyun "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial,
876*4882a593Smuzhiyun iface_desc->desc.bInterfaceNumber, dev->minor - IOWARRIOR_MINOR_BASE);
877*4882a593Smuzhiyun return retval;
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun error:
880*4882a593Smuzhiyun iowarrior_delete(dev);
881*4882a593Smuzhiyun return retval;
882*4882a593Smuzhiyun }
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun /*
885*4882a593Smuzhiyun * iowarrior_disconnect
886*4882a593Smuzhiyun *
887*4882a593Smuzhiyun * Called by the usb core when the device is removed from the system.
888*4882a593Smuzhiyun */
iowarrior_disconnect(struct usb_interface * interface)889*4882a593Smuzhiyun static void iowarrior_disconnect(struct usb_interface *interface)
890*4882a593Smuzhiyun {
891*4882a593Smuzhiyun struct iowarrior *dev = usb_get_intfdata(interface);
892*4882a593Smuzhiyun int minor = dev->minor;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun usb_deregister_dev(interface, &iowarrior_class);
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun mutex_lock(&dev->mutex);
897*4882a593Smuzhiyun
898*4882a593Smuzhiyun /* prevent device read, write and ioctl */
899*4882a593Smuzhiyun dev->present = 0;
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun if (dev->opened) {
902*4882a593Smuzhiyun /* There is a process that holds a filedescriptor to the device ,
903*4882a593Smuzhiyun so we only shutdown read-/write-ops going on.
904*4882a593Smuzhiyun Deleting the device is postponed until close() was called.
905*4882a593Smuzhiyun */
906*4882a593Smuzhiyun usb_kill_urb(dev->int_in_urb);
907*4882a593Smuzhiyun usb_kill_anchored_urbs(&dev->submitted);
908*4882a593Smuzhiyun wake_up_interruptible(&dev->read_wait);
909*4882a593Smuzhiyun wake_up_interruptible(&dev->write_wait);
910*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
911*4882a593Smuzhiyun } else {
912*4882a593Smuzhiyun /* no process is using the device, cleanup now */
913*4882a593Smuzhiyun mutex_unlock(&dev->mutex);
914*4882a593Smuzhiyun iowarrior_delete(dev);
915*4882a593Smuzhiyun }
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun dev_info(&interface->dev, "I/O-Warror #%d now disconnected\n",
918*4882a593Smuzhiyun minor - IOWARRIOR_MINOR_BASE);
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun /* usb specific object needed to register this driver with the usb subsystem */
922*4882a593Smuzhiyun static struct usb_driver iowarrior_driver = {
923*4882a593Smuzhiyun .name = "iowarrior",
924*4882a593Smuzhiyun .probe = iowarrior_probe,
925*4882a593Smuzhiyun .disconnect = iowarrior_disconnect,
926*4882a593Smuzhiyun .id_table = iowarrior_ids,
927*4882a593Smuzhiyun };
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun module_usb_driver(iowarrior_driver);
930