usblp: quirk flag and device entry for Seiko Epson M129C printer
[linux-2.6] / drivers / usb / serial / airprime.c
1 /*
2  * AirPrime CDMA Wireless Serial USB driver
3  *
4  * Copyright (C) 2005-2006 Greg Kroah-Hartman <gregkh@suse.de>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License version
8  *      2 as published by the Free Software Foundation.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/tty.h>
14 #include <linux/tty_flip.h>
15 #include <linux/module.h>
16 #include <linux/usb.h>
17 #include <linux/usb/serial.h>
18
19 static struct usb_device_id id_table [] = {
20         { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera Wireless KPC650/Passport */
21         { USB_DEVICE(0x1410, 0x1100) }, /* ExpressCard34 Qualcomm 3G CDMA */
22         { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin CDMA */
23         { USB_DEVICE(0x1410, 0x1130) }, /* Novatel Wireless S720 CDMA/EV-DO */
24         { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Merlin XU870 HSDPA/3G */
25         { USB_DEVICE(0x1410, 0x2100) }, /* Novatel Wireless EV620 CDMA/EV-DO */
26         { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless U720 CDMA/EV-DO */
27         { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless HSDPA 5500 */
28         { },
29 };
30 MODULE_DEVICE_TABLE(usb, id_table);
31
32 #define URB_TRANSFER_BUFFER_SIZE        4096
33 #define NUM_READ_URBS                   4
34 #define NUM_WRITE_URBS                  4
35 #define NUM_BULK_EPS                    3
36 #define MAX_BULK_EPS                    6
37
38 /* if overridden by the user, then use their value for the size of the
39  * read and write urbs, and the number of endpoints */
40 static int buffer_size = URB_TRANSFER_BUFFER_SIZE;
41 static int endpoints = NUM_BULK_EPS;
42 static int debug;
43 struct airprime_private {
44         spinlock_t lock;
45         int outstanding_urbs;
46         int throttled;
47         struct urb *read_urbp[NUM_READ_URBS];
48
49         /* Settings for the port */
50         int rts_state;  /* Handshaking pins (outputs) */
51         int dtr_state;
52         int cts_state;  /* Handshaking pins (inputs) */
53         int dsr_state;
54         int dcd_state;
55         int ri_state;
56 };
57
58 static int airprime_send_setup(struct usb_serial_port *port)
59 {
60         struct usb_serial *serial = port->serial;
61         struct airprime_private *priv;
62
63         dbg("%s", __FUNCTION__);
64
65         if (port->number != 0)
66                 return 0;
67
68         priv = usb_get_serial_port_data(port);
69
70         if (port->tty) {
71                 int val = 0;
72                 if (priv->dtr_state)
73                         val |= 0x01;
74                 if (priv->rts_state)
75                         val |= 0x02;
76
77                 return usb_control_msg(serial->dev,
78                                 usb_rcvctrlpipe(serial->dev, 0),
79                                 0x22,0x21,val,0,NULL,0,USB_CTRL_SET_TIMEOUT);
80         }
81
82         return 0;
83 }
84
85 static void airprime_read_bulk_callback(struct urb *urb)
86 {
87         struct usb_serial_port *port = urb->context;
88         unsigned char *data = urb->transfer_buffer;
89         struct tty_struct *tty;
90         int result;
91
92         dbg("%s - port %d", __FUNCTION__, port->number);
93
94         if (urb->status) {
95                 dbg("%s - nonzero read bulk status received: %d",
96                     __FUNCTION__, urb->status);
97                 return;
98         }
99         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
100
101         tty = port->tty;
102         if (tty && urb->actual_length) {
103                 tty_insert_flip_string (tty, data, urb->actual_length);
104                 tty_flip_buffer_push (tty);
105         }
106
107         result = usb_submit_urb (urb, GFP_ATOMIC);
108         if (result)
109                 dev_err(&port->dev, "%s - failed resubmitting read urb, error %d\n",
110                         __FUNCTION__, result);
111         return;
112 }
113
114 static void airprime_write_bulk_callback(struct urb *urb)
115 {
116         struct usb_serial_port *port = urb->context;
117         struct airprime_private *priv = usb_get_serial_port_data(port);
118         unsigned long flags;
119
120         dbg("%s - port %d", __FUNCTION__, port->number);
121
122         /* free up the transfer buffer, as usb_free_urb() does not do this */
123         kfree (urb->transfer_buffer);
124
125         if (urb->status)
126                 dbg("%s - nonzero write bulk status received: %d",
127                     __FUNCTION__, urb->status);
128         spin_lock_irqsave(&priv->lock, flags);
129         --priv->outstanding_urbs;
130         spin_unlock_irqrestore(&priv->lock, flags);
131
132         usb_serial_port_softint(port);
133 }
134
135 static int airprime_open(struct usb_serial_port *port, struct file *filp)
136 {
137         struct airprime_private *priv = usb_get_serial_port_data(port);
138         struct usb_serial *serial = port->serial;
139         struct urb *urb;
140         char *buffer = NULL;
141         int i;
142         int result = 0;
143
144         dbg("%s - port %d", __FUNCTION__, port->number);
145
146         /* initialize our private data structure if it isn't already created */
147         if (!priv) {
148                 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
149                 if (!priv) {
150                         result = -ENOMEM;
151                         goto out;
152                 }
153                 spin_lock_init(&priv->lock);
154                 usb_set_serial_port_data(port, priv);
155         }
156
157         /* Set some sane defaults */
158         priv->rts_state = 1;
159         priv->dtr_state = 1;
160
161         for (i = 0; i < NUM_READ_URBS; ++i) {
162                 buffer = kmalloc(buffer_size, GFP_KERNEL);
163                 if (!buffer) {
164                         dev_err(&port->dev, "%s - out of memory.\n",
165                                 __FUNCTION__);
166                         result = -ENOMEM;
167                         goto errout;
168                 }
169                 urb = usb_alloc_urb(0, GFP_KERNEL);
170                 if (!urb) {
171                         kfree(buffer);
172                         dev_err(&port->dev, "%s - no more urbs?\n",
173                                 __FUNCTION__);
174                         result = -ENOMEM;
175                         goto errout;
176                 }
177                 usb_fill_bulk_urb(urb, serial->dev,
178                                   usb_rcvbulkpipe(serial->dev,
179                                                   port->bulk_out_endpointAddress),
180                                   buffer, buffer_size,
181                                   airprime_read_bulk_callback, port);
182                 result = usb_submit_urb(urb, GFP_KERNEL);
183                 if (result) {
184                         usb_free_urb(urb);
185                         kfree(buffer);
186                         dev_err(&port->dev,
187                                 "%s - failed submitting read urb %d for port %d, error %d\n",
188                                 __FUNCTION__, i, port->number, result);
189                         goto errout;
190                 }
191                 /* remember this urb so we can kill it when the port is closed */
192                 priv->read_urbp[i] = urb;
193         }
194
195         airprime_send_setup(port);
196
197         goto out;
198
199  errout:
200         /* some error happened, cancel any submitted urbs and clean up anything that
201            got allocated successfully */
202
203         while (i-- != 0) {
204                 urb = priv->read_urbp[i];
205                 buffer = urb->transfer_buffer;
206                 usb_kill_urb (urb);
207                 usb_free_urb (urb);
208                 kfree (buffer);
209         }
210
211  out:
212         return result;
213 }
214
215 static void airprime_close(struct usb_serial_port *port, struct file * filp)
216 {
217         struct airprime_private *priv = usb_get_serial_port_data(port);
218         int i;
219
220         dbg("%s - port %d", __FUNCTION__, port->number);
221
222         priv->rts_state = 0;
223         priv->dtr_state = 0;
224
225         airprime_send_setup(port);
226
227         for (i = 0; i < NUM_READ_URBS; ++i) {
228                 usb_kill_urb (priv->read_urbp[i]);
229                 kfree (priv->read_urbp[i]->transfer_buffer);
230                 usb_free_urb (priv->read_urbp[i]);
231         }
232
233         /* free up private structure */
234         kfree (priv);
235         usb_set_serial_port_data(port, NULL);
236 }
237
238 static int airprime_write(struct usb_serial_port *port,
239                           const unsigned char *buf, int count)
240 {
241         struct airprime_private *priv = usb_get_serial_port_data(port);
242         struct usb_serial *serial = port->serial;
243         struct urb *urb;
244         unsigned char *buffer;
245         unsigned long flags;
246         int status;
247         dbg("%s - port %d", __FUNCTION__, port->number);
248
249         spin_lock_irqsave(&priv->lock, flags);
250         if (priv->outstanding_urbs > NUM_WRITE_URBS) {
251                 spin_unlock_irqrestore(&priv->lock, flags);
252                 dbg("%s - write limit hit\n", __FUNCTION__);
253                 return 0;
254         }
255         spin_unlock_irqrestore(&priv->lock, flags);
256         buffer = kmalloc(count, GFP_ATOMIC);
257         if (!buffer) {
258                 dev_err(&port->dev, "out of memory\n");
259                 return -ENOMEM;
260         }
261         urb = usb_alloc_urb(0, GFP_ATOMIC);
262         if (!urb) {
263                 dev_err(&port->dev, "no more free urbs\n");
264                 kfree (buffer);
265                 return -ENOMEM;
266         }
267         memcpy (buffer, buf, count);
268
269         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, buffer);
270
271         usb_fill_bulk_urb(urb, serial->dev,
272                           usb_sndbulkpipe(serial->dev,
273                                           port->bulk_out_endpointAddress),
274                           buffer, count,
275                           airprime_write_bulk_callback, port);
276
277         /* send it down the pipe */
278         status = usb_submit_urb(urb, GFP_ATOMIC);
279         if (status) {
280                 dev_err(&port->dev,
281                         "%s - usb_submit_urb(write bulk) failed with status = %d\n",
282                         __FUNCTION__, status);
283                 count = status;
284                 kfree (buffer);
285         } else {
286                 spin_lock_irqsave(&priv->lock, flags);
287                 ++priv->outstanding_urbs;
288                 spin_unlock_irqrestore(&priv->lock, flags);
289         }
290         /* we are done with this urb, so let the host driver
291          * really free it when it is finished with it */
292         usb_free_urb (urb);
293         return count;
294 }
295
296 static struct usb_driver airprime_driver = {
297         .name =         "airprime",
298         .probe =        usb_serial_probe,
299         .disconnect =   usb_serial_disconnect,
300         .id_table =     id_table,
301         .no_dynamic_id =        1,
302 };
303
304 static struct usb_serial_driver airprime_device = {
305         .driver = {
306                 .owner =        THIS_MODULE,
307                 .name =         "airprime",
308         },
309         .usb_driver =           &airprime_driver,
310         .id_table =             id_table,
311         .num_interrupt_in =     NUM_DONT_CARE,
312         .num_bulk_in =          NUM_DONT_CARE,
313         .num_bulk_out =         NUM_DONT_CARE,
314         .open =                 airprime_open,
315         .close =                airprime_close,
316         .write =                airprime_write,
317 };
318
319 static int __init airprime_init(void)
320 {
321         int retval;
322
323         airprime_device.num_ports =
324                 (endpoints > 0 && endpoints <= MAX_BULK_EPS) ? endpoints : NUM_BULK_EPS;
325         retval = usb_serial_register(&airprime_device);
326         if (retval)
327                 return retval;
328         retval = usb_register(&airprime_driver);
329         if (retval)
330                 usb_serial_deregister(&airprime_device);
331         return retval;
332 }
333
334 static void __exit airprime_exit(void)
335 {
336         dbg("%s", __FUNCTION__);
337
338         usb_deregister(&airprime_driver);
339         usb_serial_deregister(&airprime_device);
340 }
341
342 module_init(airprime_init);
343 module_exit(airprime_exit);
344 MODULE_LICENSE("GPL");
345
346 module_param(debug, bool, S_IRUGO | S_IWUSR);
347 MODULE_PARM_DESC(debug, "Debug enabled");
348 module_param(buffer_size, int, 0);
349 MODULE_PARM_DESC(buffer_size, "Size of the transfer buffers in bytes (default 4096)");
350 module_param(endpoints, int, 0);
351 MODULE_PARM_DESC(endpoints, "Number of bulk EPs to configure (default 3)");