aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial
diff options
context:
space:
mode:
authorJohan Hovold <jhovold@gmail.com>2010-05-05 17:45:24 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2010-05-20 16:21:42 -0400
commit4272568b3dd8dbad36014a107c0fbbef6400c917 (patch)
tree66549916d1ac841531ea193bc1ccb61a8aa58f77 /drivers/usb/serial
parente877048417454b0baca5d4a5aceed72a6602c3be (diff)
USB: aircable: rewrite using generic read and write implementations
Kill circular buffers for tx and rx as well as read work thread, and switch to generic kfifo-based write implementation. This is an example of how prepare_write_buffer and process_read_urb can be used to handle protocols with packet headers. Please note the diffstat which shows that the same functionality is now provided using only a tenth of the code (including whitespace and comments, though). Tested-by: Naranjo, Manuel Francisco <naranjo.manuel@gmail.com> Signed-off-by: Johan Hovold <jhovold@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb/serial')
-rw-r--r--drivers/usb/serial/aircable.c500
1 files changed, 47 insertions, 453 deletions
diff --git a/drivers/usb/serial/aircable.c b/drivers/usb/serial/aircable.c
index 4fd7af98b1ae..cac5162937b3 100644
--- a/drivers/usb/serial/aircable.c
+++ b/drivers/usb/serial/aircable.c
@@ -1,7 +1,9 @@
1/* 1/*
2 * AIRcable USB Bluetooth Dongle Driver. 2 * AIRcable USB Bluetooth Dongle Driver.
3 * 3 *
4 * Copyright (C) 2010 Johan Hovold <jhovold@gmail.com>
4 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com) 5 * Copyright (C) 2006 Manuel Francisco Naranjo (naranjo.manuel@gmail.com)
6 *
5 * This program is free software; you can redistribute it and/or modify it under 7 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the 8 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation. 9 * Free Software Foundation.
@@ -42,10 +44,10 @@
42 * 44 *
43 */ 45 */
44 46
47#include <asm/unaligned.h>
45#include <linux/tty.h> 48#include <linux/tty.h>
46#include <linux/slab.h> 49#include <linux/slab.h>
47#include <linux/tty_flip.h> 50#include <linux/tty_flip.h>
48#include <linux/circ_buf.h>
49#include <linux/usb.h> 51#include <linux/usb.h>
50#include <linux/usb/serial.h> 52#include <linux/usb/serial.h>
51 53
@@ -55,16 +57,12 @@ static int debug;
55#define AIRCABLE_VID 0x16CA 57#define AIRCABLE_VID 0x16CA
56#define AIRCABLE_USB_PID 0x1502 58#define AIRCABLE_USB_PID 0x1502
57 59
58/* write buffer size defines */
59#define AIRCABLE_BUF_SIZE 2048
60
61/* Protocol Stuff */ 60/* Protocol Stuff */
62#define HCI_HEADER_LENGTH 0x4 61#define HCI_HEADER_LENGTH 0x4
63#define TX_HEADER_0 0x20 62#define TX_HEADER_0 0x20
64#define TX_HEADER_1 0x29 63#define TX_HEADER_1 0x29
65#define RX_HEADER_0 0x00 64#define RX_HEADER_0 0x00
66#define RX_HEADER_1 0x20 65#define RX_HEADER_1 0x20
67#define MAX_HCI_FRAMESIZE 60
68#define HCI_COMPLETE_FRAME 64 66#define HCI_COMPLETE_FRAME 64
69 67
70/* rx_flags */ 68/* rx_flags */
@@ -74,8 +72,8 @@ static int debug;
74/* 72/*
75 * Version Information 73 * Version Information
76 */ 74 */
77#define DRIVER_VERSION "v1.0b2" 75#define DRIVER_VERSION "v2.0"
78#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>" 76#define DRIVER_AUTHOR "Naranjo, Manuel Francisco <naranjo.manuel@gmail.com>, Johan Hovold <jhovold@gmail.com>"
79#define DRIVER_DESC "AIRcable USB Driver" 77#define DRIVER_DESC "AIRcable USB Driver"
80 78
81/* ID table that will be registered with USB core */ 79/* ID table that will be registered with USB core */
@@ -85,225 +83,19 @@ static const struct usb_device_id id_table[] = {
85}; 83};
86MODULE_DEVICE_TABLE(usb, id_table); 84MODULE_DEVICE_TABLE(usb, id_table);
87 85
88 86static int aircable_prepare_write_buffer(struct usb_serial_port *port,
89/* Internal Structure */ 87 void **dest, size_t size, const void *src, size_t count)
90struct aircable_private {
91 spinlock_t rx_lock; /* spinlock for the receive lines */
92 struct circ_buf *tx_buf; /* write buffer */
93 struct circ_buf *rx_buf; /* read buffer */
94 int rx_flags; /* for throttilng */
95 struct work_struct rx_work; /* work cue for the receiving line */
96 struct usb_serial_port *port; /* USB port with which associated */
97};
98
99/* Private methods */
100
101/* Circular Buffer Methods, code from ti_usb_3410_5052 used */
102/*
103 * serial_buf_clear
104 *
105 * Clear out all data in the circular buffer.
106 */
107static void serial_buf_clear(struct circ_buf *cb)
108{
109 cb->head = cb->tail = 0;
110}
111
112/*
113 * serial_buf_alloc
114 *
115 * Allocate a circular buffer and all associated memory.
116 */
117static struct circ_buf *serial_buf_alloc(void)
118{
119 struct circ_buf *cb;
120 cb = kmalloc(sizeof(struct circ_buf), GFP_KERNEL);
121 if (cb == NULL)
122 return NULL;
123 cb->buf = kmalloc(AIRCABLE_BUF_SIZE, GFP_KERNEL);
124 if (cb->buf == NULL) {
125 kfree(cb);
126 return NULL;
127 }
128 serial_buf_clear(cb);
129 return cb;
130}
131
132/*
133 * serial_buf_free
134 *
135 * Free the buffer and all associated memory.
136 */
137static void serial_buf_free(struct circ_buf *cb)
138{
139 kfree(cb->buf);
140 kfree(cb);
141}
142
143/*
144 * serial_buf_data_avail
145 *
146 * Return the number of bytes of data available in the circular
147 * buffer.
148 */
149static int serial_buf_data_avail(struct circ_buf *cb)
150{
151 return CIRC_CNT(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
152}
153
154/*
155 * serial_buf_put
156 *
157 * Copy data data from a user buffer and put it into the circular buffer.
158 * Restrict to the amount of space available.
159 *
160 * Return the number of bytes copied.
161 */
162static int serial_buf_put(struct circ_buf *cb, const char *buf, int count)
163{
164 int c, ret = 0;
165 while (1) {
166 c = CIRC_SPACE_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
167 if (count < c)
168 c = count;
169 if (c <= 0)
170 break;
171 memcpy(cb->buf + cb->head, buf, c);
172 cb->head = (cb->head + c) & (AIRCABLE_BUF_SIZE-1);
173 buf += c;
174 count -= c;
175 ret = c;
176 }
177 return ret;
178}
179
180/*
181 * serial_buf_get
182 *
183 * Get data from the circular buffer and copy to the given buffer.
184 * Restrict to the amount of data available.
185 *
186 * Return the number of bytes copied.
187 */
188static int serial_buf_get(struct circ_buf *cb, char *buf, int count)
189{
190 int c, ret = 0;
191 while (1) {
192 c = CIRC_CNT_TO_END(cb->head, cb->tail, AIRCABLE_BUF_SIZE);
193 if (count < c)
194 c = count;
195 if (c <= 0)
196 break;
197 memcpy(buf, cb->buf + cb->tail, c);
198 cb->tail = (cb->tail + c) & (AIRCABLE_BUF_SIZE-1);
199 buf += c;
200 count -= c;
201 ret = c;
202 }
203 return ret;
204}
205
206/* End of circula buffer methods */
207
208static void aircable_send(struct usb_serial_port *port)
209{ 88{
210 int count, result; 89 unsigned char *buf = *dest;
211 struct aircable_private *priv = usb_get_serial_port_data(port);
212 unsigned char *buf;
213 __le16 *dbuf;
214 dbg("%s - port %d", __func__, port->number);
215 if (port->write_urb_busy)
216 return;
217
218 count = min(serial_buf_data_avail(priv->tx_buf), MAX_HCI_FRAMESIZE);
219 if (count == 0)
220 return;
221
222 buf = kzalloc(count + HCI_HEADER_LENGTH, GFP_ATOMIC);
223 if (!buf) {
224 dev_err(&port->dev, "%s- kzalloc(%d) failed.\n",
225 __func__, count + HCI_HEADER_LENGTH);
226 return;
227 }
228 90
91 count = kfifo_out_locked(&port->write_fifo, buf + HCI_HEADER_LENGTH,
92 size - HCI_HEADER_LENGTH, &port->lock);
229 buf[0] = TX_HEADER_0; 93 buf[0] = TX_HEADER_0;
230 buf[1] = TX_HEADER_1; 94 buf[1] = TX_HEADER_1;
231 dbuf = (__le16 *)&buf[2]; 95 put_unaligned_le16(count, &buf[2]);
232 *dbuf = cpu_to_le16((u16)count);
233 serial_buf_get(priv->tx_buf, buf + HCI_HEADER_LENGTH,
234 MAX_HCI_FRAMESIZE);
235
236 memcpy(port->write_urb->transfer_buffer, buf,
237 count + HCI_HEADER_LENGTH);
238
239 kfree(buf);
240 port->write_urb_busy = 1;
241 usb_serial_debug_data(debug, &port->dev, __func__,
242 count + HCI_HEADER_LENGTH,
243 port->write_urb->transfer_buffer);
244 port->write_urb->transfer_buffer_length = count + HCI_HEADER_LENGTH;
245 port->write_urb->dev = port->serial->dev;
246 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
247
248 if (result) {
249 dev_err(&port->dev,
250 "%s - failed submitting write urb, error %d\n",
251 __func__, result);
252 port->write_urb_busy = 0;
253 }
254
255 schedule_work(&port->work);
256}
257
258static void aircable_read(struct work_struct *work)
259{
260 struct aircable_private *priv =
261 container_of(work, struct aircable_private, rx_work);
262 struct usb_serial_port *port = priv->port;
263 struct tty_struct *tty;
264 unsigned char *data;
265 int count;
266 if (priv->rx_flags & THROTTLED) {
267 if (priv->rx_flags & ACTUALLY_THROTTLED)
268 schedule_work(&priv->rx_work);
269 return;
270 }
271
272 /* By now I will flush data to the tty in packages of no more than
273 * 64 bytes, to ensure I do not get throttled.
274 * Ask USB mailing list for better aproach.
275 */
276 tty = tty_port_tty_get(&port->port);
277
278 if (!tty) {
279 schedule_work(&priv->rx_work);
280 dev_err(&port->dev, "%s - No tty available\n", __func__);
281 return ;
282 }
283
284 count = min(64, serial_buf_data_avail(priv->rx_buf));
285
286 if (count <= 0)
287 goto out; /* We have finished sending everything. */
288 96
289 tty_prepare_flip_string(tty, &data, count); 97 return count;
290 if (!data) {
291 dev_err(&port->dev, "%s- kzalloc(%d) failed.",
292 __func__, count);
293 goto out;
294 }
295
296 serial_buf_get(priv->rx_buf, data, count);
297
298 tty_flip_buffer_push(tty);
299
300 if (serial_buf_data_avail(priv->rx_buf))
301 schedule_work(&priv->rx_work);
302out:
303 tty_kref_put(tty);
304 return;
305} 98}
306/* End of private methods */
307 99
308static int aircable_probe(struct usb_serial *serial, 100static int aircable_probe(struct usb_serial *serial,
309 const struct usb_device_id *id) 101 const struct usb_device_id *id)
@@ -330,247 +122,50 @@ static int aircable_probe(struct usb_serial *serial,
330 return 0; 122 return 0;
331} 123}
332 124
333static int aircable_attach(struct usb_serial *serial) 125static int aircable_process_packet(struct tty_struct *tty,
334{ 126 struct usb_serial_port *port, int has_headers,
335 struct usb_serial_port *port = serial->port[0]; 127 char *packet, int len)
336 struct aircable_private *priv;
337
338 priv = kzalloc(sizeof(struct aircable_private), GFP_KERNEL);
339 if (!priv) {
340 dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__,
341 sizeof(struct aircable_private));
342 return -ENOMEM;
343 }
344
345 /* Allocation of Circular Buffers */
346 priv->tx_buf = serial_buf_alloc();
347 if (priv->tx_buf == NULL) {
348 kfree(priv);
349 return -ENOMEM;
350 }
351
352 priv->rx_buf = serial_buf_alloc();
353 if (priv->rx_buf == NULL) {
354 kfree(priv->tx_buf);
355 kfree(priv);
356 return -ENOMEM;
357 }
358
359 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
360 priv->port = port;
361 INIT_WORK(&priv->rx_work, aircable_read);
362
363 usb_set_serial_port_data(serial->port[0], priv);
364
365 return 0;
366}
367
368static void aircable_release(struct usb_serial *serial)
369{
370
371 struct usb_serial_port *port = serial->port[0];
372 struct aircable_private *priv = usb_get_serial_port_data(port);
373
374 dbg("%s", __func__);
375
376 if (priv) {
377 serial_buf_free(priv->tx_buf);
378 serial_buf_free(priv->rx_buf);
379 kfree(priv);
380 }
381}
382
383static int aircable_write_room(struct tty_struct *tty)
384{
385 struct usb_serial_port *port = tty->driver_data;
386 struct aircable_private *priv = usb_get_serial_port_data(port);
387 return serial_buf_data_avail(priv->tx_buf);
388}
389
390static int aircable_write(struct tty_struct *tty, struct usb_serial_port *port,
391 const unsigned char *source, int count)
392{ 128{
393 struct aircable_private *priv = usb_get_serial_port_data(port); 129 if (has_headers) {
394 int temp; 130 len -= HCI_HEADER_LENGTH;
395 131 packet += HCI_HEADER_LENGTH;
396 dbg("%s - port %d, %d bytes", __func__, port->number, count);
397
398 usb_serial_debug_data(debug, &port->dev, __func__, count, source);
399
400 if (!count) {
401 dbg("%s - write request of 0 bytes", __func__);
402 return count;
403 } 132 }
404 133 if (len <= 0) {
405 temp = serial_buf_put(priv->tx_buf, source, count); 134 dbg("%s - malformed packet", __func__);
406 135 return 0;
407 aircable_send(port);
408
409 if (count > AIRCABLE_BUF_SIZE)
410 count = AIRCABLE_BUF_SIZE;
411
412 return count;
413
414}
415
416static void aircable_write_bulk_callback(struct urb *urb)
417{
418 struct usb_serial_port *port = urb->context;
419 int status = urb->status;
420 int result;
421
422 dbg("%s - urb status: %d", __func__ , status);
423
424 /* This has been taken from cypress_m8.c cypress_write_int_callback */
425 switch (status) {
426 case 0:
427 /* success */
428 break;
429 case -ECONNRESET:
430 case -ENOENT:
431 case -ESHUTDOWN:
432 /* this urb is terminated, clean up */
433 dbg("%s - urb shutting down with status: %d",
434 __func__, status);
435 port->write_urb_busy = 0;
436 return;
437 default:
438 /* error in the urb, so we have to resubmit it */
439 dbg("%s - Overflow in write", __func__);
440 dbg("%s - nonzero write bulk status received: %d",
441 __func__, status);
442 port->write_urb->transfer_buffer_length = 1;
443 port->write_urb->dev = port->serial->dev;
444 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
445 if (result)
446 dev_err(&urb->dev->dev,
447 "%s - failed resubmitting write urb, error %d\n",
448 __func__, result);
449 else
450 return;
451 } 136 }
452 137
453 port->write_urb_busy = 0; 138 tty_insert_flip_string(tty, packet, len);
454 139
455 aircable_send(port); 140 return len;
456} 141}
457 142
458static void aircable_read_bulk_callback(struct urb *urb) 143static void aircable_process_read_urb(struct urb *urb)
459{ 144{
460 struct usb_serial_port *port = urb->context; 145 struct usb_serial_port *port = urb->context;
461 struct aircable_private *priv = usb_get_serial_port_data(port); 146 char *data = (char *)urb->transfer_buffer;
462 struct tty_struct *tty; 147 struct tty_struct *tty;
463 unsigned long no_packages, remaining, package_length, i; 148 int has_headers;
464 int result, shift = 0; 149 int count;
465 unsigned char *temp; 150 int len;
466 int status = urb->status; 151 int i;
467
468 dbg("%s - port %d", __func__, port->number);
469
470 if (status) {
471 dbg("%s - urb status = %d", __func__, status);
472 if (status == -EPROTO) {
473 dbg("%s - caught -EPROTO, resubmitting the urb",
474 __func__);
475 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
476 usb_rcvbulkpipe(port->serial->dev,
477 port->bulk_in_endpointAddress),
478 port->read_urb->transfer_buffer,
479 port->read_urb->transfer_buffer_length,
480 aircable_read_bulk_callback, port);
481
482 result = usb_submit_urb(urb, GFP_ATOMIC);
483 if (result)
484 dev_err(&urb->dev->dev,
485 "%s - failed resubmitting read urb, error %d\n",
486 __func__, result);
487 return;
488 }
489 dbg("%s - unable to handle the error, exiting.", __func__);
490 return;
491 }
492
493 usb_serial_debug_data(debug, &port->dev, __func__,
494 urb->actual_length, urb->transfer_buffer);
495 152
496 tty = tty_port_tty_get(&port->port); 153 tty = tty_port_tty_get(&port->port);
497 if (tty && urb->actual_length) { 154 if (!tty)
498 if (urb->actual_length <= 2) { 155 return;
499 /* This is an incomplete package */
500 serial_buf_put(priv->rx_buf, urb->transfer_buffer,
501 urb->actual_length);
502 } else {
503 temp = urb->transfer_buffer;
504 if (temp[0] == RX_HEADER_0)
505 shift = HCI_HEADER_LENGTH;
506
507 remaining = urb->actual_length;
508 no_packages = urb->actual_length / (HCI_COMPLETE_FRAME);
509
510 if (urb->actual_length % HCI_COMPLETE_FRAME != 0)
511 no_packages++;
512 156
513 for (i = 0; i < no_packages; i++) { 157 has_headers = (urb->actual_length > 2 && data[0] == RX_HEADER_0);
514 if (remaining > (HCI_COMPLETE_FRAME))
515 package_length = HCI_COMPLETE_FRAME;
516 else
517 package_length = remaining;
518 remaining -= package_length;
519 158
520 serial_buf_put(priv->rx_buf, 159 count = 0;
521 urb->transfer_buffer + shift + 160 for (i = 0; i < urb->actual_length; i += HCI_COMPLETE_FRAME) {
522 (HCI_COMPLETE_FRAME) * (i), 161 len = min_t(int, urb->actual_length - i, HCI_COMPLETE_FRAME);
523 package_length - shift); 162 count += aircable_process_packet(tty, port, has_headers,
524 } 163 &data[i], len);
525 }
526 aircable_read(&priv->rx_work);
527 } 164 }
528 tty_kref_put(tty);
529
530 /* Schedule the next read */
531 usb_fill_bulk_urb(port->read_urb, port->serial->dev,
532 usb_rcvbulkpipe(port->serial->dev,
533 port->bulk_in_endpointAddress),
534 port->read_urb->transfer_buffer,
535 port->read_urb->transfer_buffer_length,
536 aircable_read_bulk_callback, port);
537
538 result = usb_submit_urb(urb, GFP_ATOMIC);
539 if (result && result != -EPERM)
540 dev_err(&urb->dev->dev,
541 "%s - failed resubmitting read urb, error %d\n",
542 __func__, result);
543}
544
545/* Based on ftdi_sio.c throttle */
546static void aircable_throttle(struct tty_struct *tty)
547{
548 struct usb_serial_port *port = tty->driver_data;
549 struct aircable_private *priv = usb_get_serial_port_data(port);
550
551 dbg("%s - port %d", __func__, port->number);
552 165
553 spin_lock_irq(&priv->rx_lock); 166 if (count)
554 priv->rx_flags |= THROTTLED; 167 tty_flip_buffer_push(tty);
555 spin_unlock_irq(&priv->rx_lock); 168 tty_kref_put(tty);
556}
557
558/* Based on ftdi_sio.c unthrottle */
559static void aircable_unthrottle(struct tty_struct *tty)
560{
561 struct usb_serial_port *port = tty->driver_data;
562 struct aircable_private *priv = usb_get_serial_port_data(port);
563 int actually_throttled;
564
565 dbg("%s - port %d", __func__, port->number);
566
567 spin_lock_irq(&priv->rx_lock);
568 actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
569 priv->rx_flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
570 spin_unlock_irq(&priv->rx_lock);
571
572 if (actually_throttled)
573 schedule_work(&priv->rx_work);
574} 169}
575 170
576static struct usb_driver aircable_driver = { 171static struct usb_driver aircable_driver = {
@@ -589,15 +184,14 @@ static struct usb_serial_driver aircable_device = {
589 .usb_driver = &aircable_driver, 184 .usb_driver = &aircable_driver,
590 .id_table = id_table, 185 .id_table = id_table,
591 .num_ports = 1, 186 .num_ports = 1,
592 .attach = aircable_attach, 187 .bulk_out_size = HCI_COMPLETE_FRAME,
188 /* Must modify prepare_write_buffer if multi_urb_write is changed. */
189 .multi_urb_write = 0,
593 .probe = aircable_probe, 190 .probe = aircable_probe,
594 .release = aircable_release, 191 .process_read_urb = aircable_process_read_urb,
595 .write = aircable_write, 192 .prepare_write_buffer = aircable_prepare_write_buffer,
596 .write_room = aircable_write_room, 193 .throttle = usb_serial_generic_throttle,
597 .write_bulk_callback = aircable_write_bulk_callback, 194 .unthrottle = usb_serial_generic_unthrottle,
598 .read_bulk_callback = aircable_read_bulk_callback,
599 .throttle = aircable_throttle,
600 .unthrottle = aircable_unthrottle,
601}; 195};
602 196
603static int __init aircable_init(void) 197static int __init aircable_init(void)