Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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