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