Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB Empeg empeg-car player driver |
| 3 | * |
| 4 | * Copyright (C) 2000, 2001 |
| 5 | * Gary Brubaker (xavyer@ix.netcom.com) |
| 6 | * |
| 7 | * Copyright (C) 1999 - 2001 |
| 8 | * Greg Kroah-Hartman (greg@kroah.com) |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License, as published by |
| 12 | * the Free Software Foundation, version 2. |
| 13 | * |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 14 | * See Documentation/usb/usb-serial.txt for more information on using this |
| 15 | * driver |
| 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * (07/16/2001) gb |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 18 | * remove unused code in empeg_close() (thanks to Oliver Neukum for |
| 19 | * pointing this out) and rewrote empeg_set_termios(). |
| 20 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * (05/30/2001) gkh |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 22 | * switched from using spinlock to a semaphore, which fixes lots of |
| 23 | * problems. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * |
| 25 | * (04/08/2001) gb |
| 26 | * Identify version on module load. |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 27 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * (01/22/2001) gb |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 29 | * Added write_room() and chars_in_buffer() support. |
| 30 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * (12/21/2000) gb |
| 32 | * Moved termio stuff inside the port->active check. |
| 33 | * Moved MOD_DEC_USE_COUNT to end of empeg_close(). |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 34 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | * (12/03/2000) gb |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 36 | * Added tty->ldisc.set_termios(port, tty, NULL) to empeg_open(). |
| 37 | * This notifies the tty driver that the termios have changed. |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 38 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * (11/13/2000) gb |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 40 | * Moved tty->low_latency = 1 from empeg_read_bulk_callback() to |
| 41 | * empeg_open() (It only needs to be set once - Doh!) |
| 42 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * (11/11/2000) gb |
| 44 | * Updated to work with id_table structure. |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 45 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * (11/04/2000) gb |
| 47 | * Forked this from visor.c, and hacked it up to work with an |
| 48 | * Empeg ltd. empeg-car player. Constructive criticism welcomed. |
| 49 | * I would like to say, 'Thank You' to Greg Kroah-Hartman for the |
| 50 | * use of his code, and for his guidance, advice and patience. :) |
| 51 | * A 'Thank You' is in order for John Ripley of Empeg ltd for his |
| 52 | * advice, and patience too. |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 53 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <linux/kernel.h> |
| 57 | #include <linux/errno.h> |
| 58 | #include <linux/init.h> |
| 59 | #include <linux/slab.h> |
| 60 | #include <linux/tty.h> |
| 61 | #include <linux/tty_driver.h> |
| 62 | #include <linux/tty_flip.h> |
| 63 | #include <linux/module.h> |
| 64 | #include <linux/spinlock.h> |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 65 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 67 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | static int debug; |
| 70 | |
| 71 | /* |
| 72 | * Version Information |
| 73 | */ |
| 74 | #define DRIVER_VERSION "v1.2" |
| 75 | #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Gary Brubaker <xavyer@ix.netcom.com>" |
| 76 | #define DRIVER_DESC "USB Empeg Mark I/II Driver" |
| 77 | |
| 78 | #define EMPEG_VENDOR_ID 0x084f |
| 79 | #define EMPEG_PRODUCT_ID 0x0001 |
| 80 | |
| 81 | /* function prototypes for an empeg-car player */ |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 82 | static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port, |
| 83 | struct file *filp); |
| 84 | static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port, |
| 85 | struct file *filp); |
| 86 | static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 87 | const unsigned char *buf, |
| 88 | int count); |
| 89 | static int empeg_write_room(struct tty_struct *tty); |
| 90 | static int empeg_chars_in_buffer(struct tty_struct *tty); |
| 91 | static void empeg_throttle(struct tty_struct *tty); |
| 92 | static void empeg_unthrottle(struct tty_struct *tty); |
| 93 | static int empeg_startup(struct usb_serial *serial); |
| 94 | static void empeg_shutdown(struct usb_serial *serial); |
| 95 | static void empeg_set_termios(struct tty_struct *tty, |
| 96 | struct usb_serial_port *port, struct ktermios *old_termios); |
| 97 | static void empeg_write_bulk_callback(struct urb *urb); |
| 98 | static void empeg_read_bulk_callback(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | static struct usb_device_id id_table [] = { |
| 101 | { USB_DEVICE(EMPEG_VENDOR_ID, EMPEG_PRODUCT_ID) }, |
| 102 | { } /* Terminating entry */ |
| 103 | }; |
| 104 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 105 | MODULE_DEVICE_TABLE(usb, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | static struct usb_driver empeg_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .name = "empeg", |
| 109 | .probe = usb_serial_probe, |
| 110 | .disconnect = usb_serial_disconnect, |
| 111 | .id_table = id_table, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 112 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 115 | static struct usb_serial_driver empeg_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 116 | .driver = { |
| 117 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 118 | .name = "empeg", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 119 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .id_table = id_table, |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 121 | .usb_driver = &empeg_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | .num_ports = 1, |
| 123 | .open = empeg_open, |
| 124 | .close = empeg_close, |
| 125 | .throttle = empeg_throttle, |
| 126 | .unthrottle = empeg_unthrottle, |
| 127 | .attach = empeg_startup, |
| 128 | .shutdown = empeg_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | .set_termios = empeg_set_termios, |
| 130 | .write = empeg_write, |
| 131 | .write_room = empeg_write_room, |
| 132 | .chars_in_buffer = empeg_chars_in_buffer, |
| 133 | .write_bulk_callback = empeg_write_bulk_callback, |
| 134 | .read_bulk_callback = empeg_read_bulk_callback, |
| 135 | }; |
| 136 | |
| 137 | #define NUM_URBS 16 |
| 138 | #define URB_TRANSFER_BUFFER_SIZE 4096 |
| 139 | |
| 140 | static struct urb *write_urb_pool[NUM_URBS]; |
| 141 | static spinlock_t write_urb_pool_lock; |
| 142 | static int bytes_in; |
| 143 | static int bytes_out; |
| 144 | |
| 145 | /****************************************************************************** |
| 146 | * Empeg specific driver functions |
| 147 | ******************************************************************************/ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 148 | static int empeg_open(struct tty_struct *tty, struct usb_serial_port *port, |
| 149 | struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
| 151 | struct usb_serial *serial = port->serial; |
| 152 | int result = 0; |
| 153 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 154 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* Force default termio settings */ |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 157 | empeg_set_termios(tty, port, NULL) ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | bytes_in = 0; |
| 160 | bytes_out = 0; |
| 161 | |
| 162 | /* Start reading from the device */ |
| 163 | usb_fill_bulk_urb( |
| 164 | port->read_urb, |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 165 | serial->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | usb_rcvbulkpipe(serial->dev, |
| 167 | port->bulk_in_endpointAddress), |
| 168 | port->read_urb->transfer_buffer, |
| 169 | port->read_urb->transfer_buffer_length, |
| 170 | empeg_read_bulk_callback, |
| 171 | port); |
| 172 | |
| 173 | result = usb_submit_urb(port->read_urb, GFP_KERNEL); |
| 174 | |
| 175 | if (result) |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 176 | dev_err(&port->dev, |
| 177 | "%s - failed submitting read urb, error %d\n", |
| 178 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 184 | static void empeg_close(struct tty_struct *tty, struct usb_serial_port *port, |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 185 | struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 187 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
| 189 | /* shutdown our bulk read */ |
| 190 | usb_kill_urb(port->read_urb); |
| 191 | /* Uncomment the following line if you want to see some statistics in your syslog */ |
| 192 | /* dev_info (&port->dev, "Bytes In = %d Bytes Out = %d\n", bytes_in, bytes_out); */ |
| 193 | } |
| 194 | |
| 195 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 196 | static int empeg_write(struct tty_struct *tty, struct usb_serial_port *port, |
| 197 | const unsigned char *buf, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | struct usb_serial *serial = port->serial; |
| 200 | struct urb *urb; |
| 201 | const unsigned char *current_position = buf; |
| 202 | unsigned long flags; |
| 203 | int status; |
| 204 | int i; |
| 205 | int bytes_sent = 0; |
| 206 | int transfer_size; |
| 207 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 208 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | while (count > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | /* try to find a free urb in our list of them */ |
| 212 | urb = NULL; |
| 213 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 214 | spin_lock_irqsave(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
| 216 | for (i = 0; i < NUM_URBS; ++i) { |
| 217 | if (write_urb_pool[i]->status != -EINPROGRESS) { |
| 218 | urb = write_urb_pool[i]; |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 223 | spin_unlock_irqrestore(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | if (urb == NULL) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 226 | dbg("%s - no more free urbs", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | goto exit; |
| 228 | } |
| 229 | |
| 230 | if (urb->transfer_buffer == NULL) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 231 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | if (urb->transfer_buffer == NULL) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 233 | dev_err(&port->dev, |
| 234 | "%s no more kernel memory...\n", |
| 235 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | goto exit; |
| 237 | } |
| 238 | } |
| 239 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 240 | transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 242 | memcpy(urb->transfer_buffer, current_position, transfer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 244 | usb_serial_debug_data(debug, &port->dev, __func__, transfer_size, urb->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | /* build up our urb */ |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 247 | usb_fill_bulk_urb( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | urb, |
| 249 | serial->dev, |
| 250 | usb_sndbulkpipe(serial->dev, |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 251 | port->bulk_out_endpointAddress), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | urb->transfer_buffer, |
| 253 | transfer_size, |
| 254 | empeg_write_bulk_callback, |
| 255 | port); |
| 256 | |
| 257 | /* send it down the pipe */ |
| 258 | status = usb_submit_urb(urb, GFP_ATOMIC); |
| 259 | if (status) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 260 | dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed with status = %d\n", __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | bytes_sent = status; |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | current_position += transfer_size; |
| 266 | bytes_sent += transfer_size; |
| 267 | count -= transfer_size; |
| 268 | bytes_out += transfer_size; |
| 269 | |
| 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | exit: |
| 272 | return bytes_sent; |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 273 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 276 | static int empeg_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 278 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | unsigned long flags; |
| 280 | int i; |
| 281 | int room = 0; |
| 282 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 283 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 285 | spin_lock_irqsave(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* tally up the number of bytes available */ |
| 287 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 288 | if (write_urb_pool[i]->status != -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | room += URB_TRANSFER_BUFFER_SIZE; |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 290 | } |
| 291 | spin_unlock_irqrestore(&write_urb_pool_lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 292 | dbg("%s - returns %d", __func__, room); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 293 | return room; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
| 295 | } |
| 296 | |
| 297 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 298 | static int empeg_chars_in_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 300 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | unsigned long flags; |
| 302 | int i; |
| 303 | int chars = 0; |
| 304 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 305 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 307 | spin_lock_irqsave(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | /* tally up the number of bytes waiting */ |
| 310 | for (i = 0; i < NUM_URBS; ++i) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 311 | if (write_urb_pool[i]->status == -EINPROGRESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | chars += URB_TRANSFER_BUFFER_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 315 | spin_unlock_irqrestore(&write_urb_pool_lock, flags); |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 316 | dbg("%s - returns %d", __func__, chars); |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 317 | return chars; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 321 | static void empeg_write_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Greg Kroah-Hartman | 335202f | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 323 | struct usb_serial_port *port = urb->context; |
| 324 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 326 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Greg Kroah-Hartman | 335202f | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 328 | if (status) { |
| 329 | dbg("%s - nonzero write bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 330 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Pete Zaitcev | cf2c748 | 2006-05-22 21:58:49 -0700 | [diff] [blame] | 334 | usb_serial_port_softint(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 338 | static void empeg_read_bulk_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Ming Lei | cdc9779 | 2008-02-24 18:41:47 +0800 | [diff] [blame] | 340 | struct usb_serial_port *port = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | struct tty_struct *tty; |
| 342 | unsigned char *data = urb->transfer_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | int result; |
Greg Kroah-Hartman | 335202f | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 344 | int status = urb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 346 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Greg Kroah-Hartman | 335202f | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 348 | if (status) { |
| 349 | dbg("%s - nonzero read bulk status received: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 350 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return; |
| 352 | } |
| 353 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 354 | usb_serial_debug_data(debug, &port->dev, __func__, |
| 355 | urb->actual_length, data); |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 356 | tty = tty_port_tty_get(&port->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | if (urb->actual_length) { |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 359 | tty_buffer_request_room(tty, urb->actual_length); |
| 360 | tty_insert_flip_string(tty, data, urb->actual_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | tty_flip_buffer_push(tty); |
| 362 | bytes_in += urb->actual_length; |
| 363 | } |
Alan Cox | 4a90f09 | 2008-10-13 10:39:46 +0100 | [diff] [blame] | 364 | tty_kref_put(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | /* Continue trying to always read */ |
| 367 | usb_fill_bulk_urb( |
| 368 | port->read_urb, |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 369 | port->serial->dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | usb_rcvbulkpipe(port->serial->dev, |
| 371 | port->bulk_in_endpointAddress), |
| 372 | port->read_urb->transfer_buffer, |
| 373 | port->read_urb->transfer_buffer_length, |
| 374 | empeg_read_bulk_callback, |
| 375 | port); |
| 376 | |
| 377 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
| 378 | |
| 379 | if (result) |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 380 | dev_err(&urb->dev->dev, |
| 381 | "%s - failed resubmitting read urb, error %d\n", |
| 382 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | return; |
| 385 | |
| 386 | } |
| 387 | |
| 388 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 389 | static void empeg_throttle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 391 | struct usb_serial_port *port = tty->driver_data; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 392 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | usb_kill_urb(port->read_urb); |
| 394 | } |
| 395 | |
| 396 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 397 | static void empeg_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 399 | struct usb_serial_port *port = tty->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | int result; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 401 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
| 403 | port->read_urb->dev = port->serial->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | result = usb_submit_urb(port->read_urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | if (result) |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 406 | dev_err(&port->dev, |
| 407 | "%s - failed submitting read urb, error %d\n", |
| 408 | __func__, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 412 | static int empeg_startup(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
| 414 | int r; |
| 415 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 416 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | if (serial->dev->actconfig->desc.bConfigurationValue != 1) { |
| 419 | err("active config #%d != 1 ??", |
| 420 | serial->dev->actconfig->desc.bConfigurationValue); |
| 421 | return -ENODEV; |
| 422 | } |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 423 | dbg("%s - reset config", __func__); |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 424 | r = usb_reset_configuration(serial->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | /* continue on with initialization */ |
| 427 | return r; |
| 428 | |
| 429 | } |
| 430 | |
| 431 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 432 | static void empeg_shutdown(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 434 | dbg("%s", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 438 | static void empeg_set_termios(struct tty_struct *tty, |
| 439 | struct usb_serial_port *port, struct ktermios *old_termios) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 441 | struct ktermios *termios = tty->termios; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 442 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | /* |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 445 | * The empeg-car player wants these particular tty settings. |
| 446 | * You could, for example, change the baud rate, however the |
| 447 | * player only supports 115200 (currently), so there is really |
| 448 | * no point in support for changes to the tty settings. |
| 449 | * (at least for now) |
| 450 | * |
| 451 | * The default requirements for this device are: |
| 452 | */ |
Alan Cox | 998e863 | 2007-10-18 01:24:19 -0700 | [diff] [blame] | 453 | termios->c_iflag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | &= ~(IGNBRK /* disable ignore break */ |
| 455 | | BRKINT /* disable break causes interrupt */ |
| 456 | | PARMRK /* disable mark parity errors */ |
| 457 | | ISTRIP /* disable clear high bit of input characters */ |
| 458 | | INLCR /* disable translate NL to CR */ |
| 459 | | IGNCR /* disable ignore CR */ |
| 460 | | ICRNL /* disable translate CR to NL */ |
| 461 | | IXON); /* disable enable XON/XOFF flow control */ |
| 462 | |
Alan Cox | 998e863 | 2007-10-18 01:24:19 -0700 | [diff] [blame] | 463 | termios->c_oflag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | &= ~OPOST; /* disable postprocess output characters */ |
| 465 | |
Alan Cox | 998e863 | 2007-10-18 01:24:19 -0700 | [diff] [blame] | 466 | termios->c_lflag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | &= ~(ECHO /* disable echo input characters */ |
| 468 | | ECHONL /* disable echo new line */ |
| 469 | | ICANON /* disable erase, kill, werase, and rprnt special characters */ |
| 470 | | ISIG /* disable interrupt, quit, and suspend special characters */ |
| 471 | | IEXTEN); /* disable non-POSIX special characters */ |
| 472 | |
Alan Cox | 998e863 | 2007-10-18 01:24:19 -0700 | [diff] [blame] | 473 | termios->c_cflag |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | &= ~(CSIZE /* no size */ |
| 475 | | PARENB /* disable parity bit */ |
| 476 | | CBAUD); /* clear current baud rate */ |
| 477 | |
Alan Cox | 998e863 | 2007-10-18 01:24:19 -0700 | [diff] [blame] | 478 | termios->c_cflag |
| 479 | |= CS8; /* character size 8 bits */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Force low_latency on; otherwise the pushes are scheduled; |
| 483 | * this is bad as it opens up the possibility of dropping bytes |
| 484 | * on the floor. We don't want to drop bytes on the floor. :) |
| 485 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 486 | tty->low_latency = 1; |
| 487 | tty_encode_baud_rate(tty, 115200, 115200); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 491 | static int __init empeg_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | { |
| 493 | struct urb *urb; |
| 494 | int i, retval; |
| 495 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 496 | /* create our write urb pool and transfer buffers */ |
| 497 | spin_lock_init(&write_urb_pool_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | for (i = 0; i < NUM_URBS; ++i) { |
| 499 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 500 | write_urb_pool[i] = urb; |
| 501 | if (urb == NULL) { |
| 502 | err("No more urbs???"); |
| 503 | continue; |
| 504 | } |
| 505 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 506 | urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE, |
| 507 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | if (!urb->transfer_buffer) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 509 | err("%s - out of memory for urb buffers.", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 510 | __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | continue; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | retval = usb_serial_register(&empeg_device); |
| 516 | if (retval) |
| 517 | goto failed_usb_serial_register; |
| 518 | retval = usb_register(&empeg_driver); |
| 519 | if (retval) |
| 520 | goto failed_usb_register; |
| 521 | |
| 522 | info(DRIVER_VERSION ":" DRIVER_DESC); |
| 523 | |
| 524 | return 0; |
| 525 | failed_usb_register: |
| 526 | usb_serial_deregister(&empeg_device); |
| 527 | failed_usb_serial_register: |
| 528 | for (i = 0; i < NUM_URBS; ++i) { |
| 529 | if (write_urb_pool[i]) { |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 530 | kfree(write_urb_pool[i]->transfer_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | usb_free_urb(write_urb_pool[i]); |
| 532 | } |
| 533 | } |
| 534 | return retval; |
| 535 | } |
| 536 | |
| 537 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 538 | static void __exit empeg_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
| 540 | int i; |
| 541 | unsigned long flags; |
| 542 | |
| 543 | usb_deregister(&empeg_driver); |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 544 | usb_serial_deregister(&empeg_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 546 | spin_lock_irqsave(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | for (i = 0; i < NUM_URBS; ++i) { |
| 549 | if (write_urb_pool[i]) { |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 550 | /* FIXME - uncomment the following usb_kill_urb call |
| 551 | * when the host controllers get fixed to set urb->dev |
| 552 | * = NULL after the urb is finished. Otherwise this |
| 553 | * call oopses. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | /* usb_kill_urb(write_urb_pool[i]); */ |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 555 | kfree(write_urb_pool[i]->transfer_buffer); |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 556 | usb_free_urb(write_urb_pool[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 559 | spin_unlock_irqrestore(&write_urb_pool_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | |
| 563 | module_init(empeg_init); |
| 564 | module_exit(empeg_exit); |
| 565 | |
Alan Cox | 93c4679 | 2008-07-22 11:11:11 +0100 | [diff] [blame] | 566 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 567 | MODULE_DESCRIPTION(DRIVER_DESC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | MODULE_LICENSE("GPL"); |
| 569 | |
| 570 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 571 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |