source: src/linux/xscale/linux-2.6.23/drivers/usb/serial/sierra.c @ 11999

Last change on this file since 11999 was 11999, checked in by BrainSlayer, 4 years ago

openrb powerpc board support (turned over from old magicbox codebase)

File size: 22.4 KB
Line 
1/*
2  USB Driver for Sierra Wireless
3
4  Copyright (C) 2006, 2007, 2008  Kevin Lloyd <klloyd@sierrawireless.com>
5
6  IMPORTANT DISCLAIMER: This driver is not commercially supported by
7  Sierra Wireless. Use at your own risk.
8
9  This driver is free software; you can redistribute it and/or modify
10  it under the terms of Version 2 of the GNU General Public License as
11  published by the Free Software Foundation.
12
13  Portions based on the option driver by Matthias Urlichs <smurf@smurf.noris.de>
14  Whom based his on the Keyspan driver by Hugh Blemings <hugh@blemings.org>
15
16  Back ported to kernel 2.6.23
17*/
18
19#define DRIVER_VERSION "v.1.3.1b"
20#define DRIVER_AUTHOR "Kevin Lloyd <klloyd@sierrawireless.com>"
21#define DRIVER_DESC "USB Driver for Sierra Wireless USB modems"
22
23#include <linux/kernel.h>
24#include <linux/jiffies.h>
25#include <linux/errno.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/module.h>
29#include <linux/usb.h>
30#include <linux/usb/serial.h>
31#include <linux/usb/ch9.h>
32
33#define SWIMS_USB_REQUEST_SetPower      0x00
34#define SWIMS_USB_REQUEST_SetNmea       0x07
35#define SWIMS_USB_REQUEST_SetMode       0x0B
36#define SWIMS_SET_MODE_Modem            0x0001
37
38/* per port private data */
39#define N_IN_URB        8
40#define N_OUT_URB       64
41#define IN_BUFLEN       4096
42
43
44#define MAX_TRANSFER (PAGE_SIZE - 512)
45
46static int debug;
47static int nmea;
48static int truinstall = 1;
49
50enum devicetype {
51        DEVICE_3_PORT =         0,
52        DEVICE_1_PORT =         1,
53        DEVICE_INSTALLER =      2,
54};
55
56static int sierra_set_power_state(struct usb_device *udev, __u16 swiState)
57{
58        int result;
59        dev_dbg(&udev->dev, "%s", __func__);
60        result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
61                        SWIMS_USB_REQUEST_SetPower,     /* __u8 request      */
62                        USB_TYPE_VENDOR,                /* __u8 request type */
63                        swiState,                       /* __u16 value       */
64                        0,                              /* __u16 index       */
65                        NULL,                           /* void *data        */
66                        0,                              /* __u16 size        */
67                        USB_CTRL_SET_TIMEOUT);          /* int timeout       */
68        return result;
69}
70
71static int sierra_set_ms_mode(struct usb_device *udev, __u16 eSWocMode)
72{
73        int result;
74        dev_dbg(&udev->dev, "%s", "DEVICE MODE SWITCH\n");
75        result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
76                        SWIMS_USB_REQUEST_SetMode,      /* __u8 request      */
77                        USB_TYPE_VENDOR,                /* __u8 request type */
78                        eSWocMode,                      /* __u16 value       */
79                        0x0000,                         /* __u16 index       */
80                        NULL,                           /* void *data        */
81                        0,                              /* __u16 size        */
82                        USB_CTRL_SET_TIMEOUT);          /* int timeout       */
83        return result;
84}
85
86static int sierra_vsc_set_nmea(struct usb_device *udev, __u16 enable)
87{
88        int result;
89        dev_dbg(&udev->dev, "%s", __func__);
90        result = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
91                        SWIMS_USB_REQUEST_SetNmea,      /* __u8 request      */
92                        USB_TYPE_VENDOR,                /* __u8 request type */
93                        enable,                         /* __u16 value       */
94                        0x0000,                         /* __u16 index       */
95                        NULL,                           /* void *data        */
96                        0,                              /* __u16 size        */
97                        USB_CTRL_SET_TIMEOUT);          /* int timeout       */
98        return result;
99}
100
101static int sierra_calc_num_ports(struct usb_serial *serial)
102{
103        int result;
104        int *num_ports = usb_get_serial_data(serial);
105        dev_dbg(&serial->dev->dev, "%s", __func__);
106
107        result = *num_ports;
108
109        if (result) {
110                kfree(num_ports);
111                usb_set_serial_data(serial, NULL);
112        }
113
114        return result;
115}
116
117static int sierra_calc_interface(struct usb_serial *serial)
118{
119        int interface;
120        struct usb_interface *p_interface;
121        struct usb_host_interface *p_host_interface;
122        dev_dbg(&serial->dev->dev, "%s", __func__);
123
124        /* Get the interface structure pointer from the serial struct */
125        p_interface = serial->interface;
126
127        /* Get a pointer to the host interface structure */
128        p_host_interface = p_interface->cur_altsetting;
129
130        /* read the interface descriptor for this active altsetting
131         * to find out the interface number we are on
132        */
133        interface = p_host_interface->desc.bInterfaceNumber;
134
135        return interface;
136}
137
138static int sierra_probe(struct usb_serial *serial,
139                        const struct usb_device_id *id)
140{
141        int result = 0;
142        struct usb_device *udev;
143        int *num_ports;
144        u8 ifnum, ifclass, numendpoints;
145
146        dev_dbg(&serial->dev->dev, "%s", __func__);
147
148        num_ports = kmalloc(sizeof(*num_ports), GFP_KERNEL);
149        if (!num_ports)
150                return -ENOMEM;
151
152        ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
153        ifclass = serial->interface->cur_altsetting->desc.bInterfaceClass;
154        numendpoints = serial->interface->cur_altsetting->desc.bNumEndpoints;
155        udev = serial->dev;
156
157        /* Figure out the interface number from the serial structure */
158        ifnum = sierra_calc_interface(serial);
159
160        /*
161         * If this interface supports more than 1 alternate
162         * select the 2nd one
163         */
164        if (serial->interface->num_altsetting == 2) {
165                dev_dbg(&udev->dev, "Selecting alt setting for interface %d\n",
166                        ifnum);
167                /* We know the alternate setting is 1 for the MC8785 */
168                usb_set_interface(udev, ifnum, 1);
169        }
170
171        if (ifclass == USB_CLASS_MASS_STORAGE) {
172                /* If TRU-Install support is enabled, force to modem mode */
173                if (truinstall && id->driver_info == DEVICE_INSTALLER) {
174                        dev_dbg(&udev->dev, "%s", "FOUND TRU-INSTALL DEVICE\n");
175                        result = sierra_set_ms_mode(udev, SWIMS_SET_MODE_Modem);
176                }
177                kfree(num_ports);
178                return -EIO;
179        /* Dummy interface present on some SKUs should be ignored */
180        } else if (ifnum == 0x99)
181                *num_ports = 0;
182        else if (numendpoints <= 3)
183                *num_ports = 1;
184        else
185                *num_ports = (numendpoints-1)/2;
186
187        /*
188         * save off our num_ports info so that we can use it in the
189         * calc_num_ports callback
190         */
191        usb_set_serial_data(serial, (void *)num_ports);
192
193        return result;
194}
195
196static struct usb_device_id id_table [] = {
197        { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
198        { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
199        { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
200        { USB_DEVICE(0x03f0, 0x1b1d) }, /* HP ev2200 a.k.a MC5720 */
201        { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
202        { USB_DEVICE(0x1199, 0x0024) }, /* Sierra Wireless MC5727 */
203        { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
204        { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
205        { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
206        { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless USB Dongle 595U */
207         /* Sierra Wireless C597 */
208        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0023, 0xFF, 0xFF, 0xFF) },
209         /* Sierra Wireless Device */
210        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x0025, 0xFF, 0xFF, 0xFF) },
211        { USB_DEVICE(0x1199, 0x0026) }, /* Sierra Wireless Device */
212        { USB_DEVICE(0x1199, 0x0027) }, /* Sierra Wireless Device */
213        { USB_DEVICE(0x1199, 0x0028) }, /* Sierra Wireless Device */
214
215        { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
216        { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
217        { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
218        { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 & AC 875U */
219        { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8775 (Lenovo) */
220        { USB_DEVICE(0x1199, 0x6815) }, /* Sierra Wireless MC8775 */
221        { USB_DEVICE(0x03f0, 0x1e1d) }, /* HP hs2300 a.k.a MC8775 */
222        { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
223        { USB_DEVICE(0x1199, 0x6821) }, /* Sierra Wireless AirCard 875U */
224        { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
225        { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
226        { USB_DEVICE(0x1199, 0x683A) }, /* Sierra Wireless MC8785 */
227        { USB_DEVICE(0x1199, 0x683B) }, /* Sierra Wireless MC8785 Composite */
228        { USB_DEVICE(0x1199, 0x683C) }, /* Sierra Wireless MC8790 */
229        { USB_DEVICE(0x1199, 0x683D) }, /* Sierra Wireless MC8790 */
230        { USB_DEVICE(0x1199, 0x683E) }, /* Sierra Wireless MC8790 */
231        { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
232        { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
233        { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880 E */
234        { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881 E */
235        { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880 U */
236        { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881 U */
237        { USB_DEVICE(0x1199, 0x6859) }, /* Sierra Wireless AirCard 885 E */
238        { USB_DEVICE(0x1199, 0x685A) }, /* Sierra Wireless AirCard 885 E */
239        /* Sierra Wireless C885 */
240        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6880, 0xFF, 0xFF, 0xFF)},
241        /* Sierra Wireless Device */
242        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6890, 0xFF, 0xFF, 0xFF)},
243        /* Sierra Wireless Device */
244        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6891, 0xFF, 0xFF, 0xFF)},
245        /* Sierra Wireless Device */
246        { USB_DEVICE_AND_INTERFACE_INFO(0x1199, 0x6892, 0xFF, 0xFF, 0xFF)},
247
248        { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
249        { USB_DEVICE(0x0F3D, 0x0112) }, /* Airprime/Sierra PC 5220 */
250        { USB_DEVICE(0x1199, 0x68A3) }, /* Sierra Wireless Direct IP modems */
251        { USB_DEVICE(0x05C6, 0x6613) }, /* Onda H600/ZTE MF330 */
252
253        { USB_DEVICE(0x1199, 0x0FFF), .driver_info = DEVICE_INSTALLER},
254        { }
255};
256MODULE_DEVICE_TABLE(usb, id_table);
257
258static struct usb_driver sierra_driver = {
259        .name       = "sierra",
260        .probe      = usb_serial_probe,
261        .disconnect = usb_serial_disconnect,
262        .id_table   = id_table,
263};
264
265struct sierra_port_private {
266        spinlock_t lock;        /* lock the structure */
267        int outstanding_urbs;   /* number of out urbs in flight */
268
269        /* Input endpoints and buffers for this port */
270        struct urb *in_urbs[N_IN_URB];
271        char *in_buffer[N_IN_URB];
272
273        /* Settings for the port */
274        int rts_state;  /* Handshaking pins (outputs) */
275        int dtr_state;
276        int cts_state;  /* Handshaking pins (inputs) */
277        int dsr_state;
278        int dcd_state;
279        int ri_state;
280};
281
282static int sierra_send_setup(struct usb_serial_port *port)
283{
284        struct usb_serial *serial = port->serial;
285        struct sierra_port_private *portdata;
286        __u16 interface = 0;
287
288        dbg("%s", __FUNCTION__);
289
290        portdata = usb_get_serial_port_data(port);
291
292        if (port->tty) {
293                int val = 0;
294                if (portdata->dtr_state)
295                        val |= 0x01;
296                if (portdata->rts_state)
297                        val |= 0x02;
298
299                /* If composite device then properly report interface */
300                if (serial->num_ports == 1)
301                        interface = sierra_calc_interface(serial);
302
303                /* Otherwise the need to do non-composite mapping */
304                else {
305                        if (port->bulk_out_endpointAddress == 2)
306                                interface = 0;
307                        else if (port->bulk_out_endpointAddress == 4)
308                                interface = 1;
309                        else if (port->bulk_out_endpointAddress == 5)
310                                interface = 2;
311                }
312
313                return usb_control_msg(serial->dev,
314                                usb_rcvctrlpipe(serial->dev, 0),
315                                0x22, 0x21, val, interface,
316                                NULL, 0, USB_CTRL_SET_TIMEOUT);
317        }
318
319        return 0;
320}
321
322static void sierra_set_termios(struct usb_serial_port *port,
323                        struct ktermios *old_termios)
324{
325        dev_dbg(&port->dev, "%s", __func__);
326        sierra_send_setup(port);
327}
328
329static int sierra_tiocmget(struct usb_serial_port *port, struct file *file)
330{
331        unsigned int value;
332        struct sierra_port_private *portdata;
333
334        portdata = usb_get_serial_port_data(port);
335
336        value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
337                ((portdata->dtr_state) ? TIOCM_DTR : 0) |
338                ((portdata->cts_state) ? TIOCM_CTS : 0) |
339                ((portdata->dsr_state) ? TIOCM_DSR : 0) |
340                ((portdata->dcd_state) ? TIOCM_CAR : 0) |
341                ((portdata->ri_state) ? TIOCM_RNG : 0);
342
343        return value;
344}
345
346static int sierra_tiocmset(struct usb_serial_port *port, struct file *file,
347                        unsigned int set, unsigned int clear)
348{
349        struct sierra_port_private *portdata;
350
351        portdata = usb_get_serial_port_data(port);
352
353        if (set & TIOCM_RTS)
354                portdata->rts_state = 1;
355        if (set & TIOCM_DTR)
356                portdata->dtr_state = 1;
357
358        if (clear & TIOCM_RTS)
359                portdata->rts_state = 0;
360        if (clear & TIOCM_DTR)
361                portdata->dtr_state = 0;
362        return sierra_send_setup(port);
363}
364
365static void sierra_outdat_callback(struct urb *urb)
366{
367        struct usb_serial_port *port = urb->context;
368        struct sierra_port_private *portdata = usb_get_serial_port_data(port);
369        int status = urb->status;
370        unsigned long flags;
371
372        dev_dbg(&port->dev, "%s - port %d", __func__, port->number);
373
374        /* free up the transfer buffer, as usb_free_urb() does not do this */
375        kfree(urb->transfer_buffer);
376
377        if (status)
378                dev_dbg(&port->dev, "%s - nonzero write bulk status "
379                    "received: %d", __func__, status);
380
381        spin_lock_irqsave(&portdata->lock, flags);
382        --portdata->outstanding_urbs;
383        spin_unlock_irqrestore(&portdata->lock, flags);
384
385        usb_serial_port_softint(port);
386}
387
388/* Write */
389static int sierra_write(struct usb_serial_port *port,
390                        const unsigned char *buf, int count)
391{
392        struct sierra_port_private *portdata = usb_get_serial_port_data(port);
393        struct usb_serial *serial = port->serial;
394        unsigned long flags;
395        unsigned char *buffer;
396        struct urb *urb;
397        size_t writesize = min((size_t)count, (size_t)MAX_TRANSFER);
398        int retval = 0;
399       
400        if (count==0)
401            return 0;
402
403        portdata = usb_get_serial_port_data(port);
404
405        dev_dbg(&port->dev, "%s: write (%d chars)", __func__, count);
406
407        spin_lock_irqsave(&portdata->lock, flags);
408        if (portdata->outstanding_urbs > N_OUT_URB) {
409                spin_unlock_irqrestore(&portdata->lock, flags);
410                dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
411                return 0;
412        }
413        portdata->outstanding_urbs++;
414        spin_unlock_irqrestore(&portdata->lock, flags);
415
416        buffer = kmalloc(writesize, GFP_ATOMIC);
417        if (!buffer) {
418                dev_err(&port->dev, "out of memory\n");
419                retval = -ENOMEM;
420                goto error_no_buffer;
421        }
422
423        urb = usb_alloc_urb(0, GFP_ATOMIC);
424        if (!urb) {
425                dev_err(&port->dev, "no more free urbs\n");
426                retval = -ENOMEM;
427                goto error_no_urb;
428        }
429
430        memcpy(buffer, buf, writesize);
431
432        usb_serial_debug_data(debug, &port->dev, __func__, writesize, buffer);
433
434        usb_fill_bulk_urb(urb, serial->dev,
435                          usb_sndbulkpipe(serial->dev,
436                                          port->bulk_out_endpointAddress),
437                          buffer, writesize, sierra_outdat_callback, port);
438
439        /* send it down the pipe */
440        retval = usb_submit_urb(urb, GFP_ATOMIC);
441        if (retval) {
442                dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
443                        "with status = %d\n", __func__, retval);
444                goto error;
445        }
446
447        /* we are done with this urb, so let the host driver
448         * really free it when it is finished with it */
449        usb_free_urb(urb);
450
451        return writesize;
452error:
453        usb_free_urb(urb);
454error_no_urb:
455        kfree(buffer);
456error_no_buffer:
457        spin_lock_irqsave(&portdata->lock, flags);
458        --portdata->outstanding_urbs;
459        spin_unlock_irqrestore(&portdata->lock, flags);
460        return retval;
461}
462
463static void sierra_indat_callback(struct urb *urb)
464{
465        int err;
466        int endpoint;
467        struct usb_serial_port *port;
468        struct tty_struct *tty;
469        unsigned char *data = urb->transfer_buffer;
470        int status = urb->status;
471
472        dbg("%s: %p", __func__, urb);
473
474        endpoint = usb_pipeendpoint(urb->pipe);
475        port =  urb->context;
476
477        if (status) {
478                dev_dbg(&port->dev, "%s: nonzero status: %d on"
479                    " endpoint %02x.", __func__, status, endpoint);
480        } else {
481                tty = port->tty;
482                if (urb->actual_length) {
483                        tty_buffer_request_room(tty, urb->actual_length);
484                        tty_insert_flip_string(tty, data, urb->actual_length);
485                        tty_flip_buffer_push(tty);
486                } else {
487                        dev_dbg(&port->dev, "%s: empty read urb"
488                                " received", __func__);
489                }
490
491                /* Resubmit urb so we continue receiving */
492                if (port->open_count && status != -ESHUTDOWN) {
493                        err = usb_submit_urb(urb, GFP_ATOMIC);
494                        if (err)
495                                dev_err(&port->dev, "resubmit read urb failed."
496                                        "(%d)\n", err);
497                }
498        }
499        return;
500}
501
502static void sierra_instat_callback(struct urb *urb)
503{
504        int err;
505        int status = urb->status;
506        struct usb_serial_port *port =  urb->context;
507        struct sierra_port_private *portdata = usb_get_serial_port_data(port);
508        struct usb_serial *serial = port->serial;
509
510        dev_dbg(&port->dev, "%s", __func__);
511        dev_dbg(&port->dev, "%s: urb %p port %p has data %p", __func__,
512                urb, port, portdata);
513
514        if (status == 0) {
515                struct usb_ctrlrequest *req_pkt =
516                                (struct usb_ctrlrequest *)urb->transfer_buffer;
517
518                if (!req_pkt) {
519                        dev_dbg(&port->dev, "%s: NULL req_pkt\n",
520                                __func__);
521                        return;
522                }
523                if ((req_pkt->bRequestType == 0xA1) &&
524                                (req_pkt->bRequest == 0x20)) {
525                        int old_dcd_state;
526                        unsigned char signals = *((unsigned char *)
527                                        urb->transfer_buffer +
528                                        sizeof(struct usb_ctrlrequest));
529
530                        dev_dbg(&port->dev, "%s: signal x%x", __func__,
531                                signals);
532
533                        old_dcd_state = portdata->dcd_state;
534                        portdata->cts_state = 1;
535                        portdata->dcd_state = ((signals & 0x01) ? 1 : 0);
536                        portdata->dsr_state = ((signals & 0x02) ? 1 : 0);
537                        portdata->ri_state = ((signals & 0x08) ? 1 : 0);
538
539                        if (port->tty && !C_CLOCAL(port->tty) &&
540                                        old_dcd_state && !portdata->dcd_state)
541                                tty_hangup(port->tty);
542                } else {
543                        dev_dbg(&port->dev, "%s: type %x req %x",
544                                __func__, req_pkt->bRequestType,
545                                req_pkt->bRequest);
546                }
547        } else
548                dev_dbg(&port->dev, "%s: error %d", __func__, status);
549
550        /* Resubmit urb so we continue receiving IRQ data */
551        if (status != -ESHUTDOWN) {
552                urb->dev = serial->dev;
553                err = usb_submit_urb(urb, GFP_ATOMIC);
554                if (err)
555                        dev_dbg(&port->dev, "%s: resubmit intr urb "
556                                "failed. (%d)", __func__, err);
557        }
558}
559
560static int sierra_write_room(struct usb_serial_port *port)
561{
562        struct sierra_port_private *portdata = usb_get_serial_port_data(port);
563        unsigned long flags;
564
565        dev_dbg(&port->dev, "%s - port %d", __func__, port->number);
566
567        /* try to give a good number back based on if we have any free urbs at
568         * this point in time */
569        spin_lock_irqsave(&portdata->lock, flags);
570        if (portdata->outstanding_urbs > N_OUT_URB * 2 / 3) {
571                spin_unlock_irqrestore(&portdata->lock, flags);
572                dev_dbg(&port->dev, "%s - write limit hit\n", __func__);
573                return 0;
574        }
575        spin_unlock_irqrestore(&portdata->lock, flags);
576
577        return 2048;
578}
579
580static int sierra_open(struct usb_serial_port *port, struct file *filp)
581{
582        struct sierra_port_private *portdata;
583        struct usb_serial *serial = port->serial;
584        int i;
585        struct urb *urb;
586        int result;
587
588        portdata = usb_get_serial_port_data(port);
589
590        dev_dbg(&port->dev, "%s", __func__);
591
592        /* Set some sane defaults */
593        portdata->rts_state = 1;
594        portdata->dtr_state = 1;
595
596        /* Reset low level data toggle and start reading from endpoints */
597        for (i = 0; i < N_IN_URB; i++) {
598                urb = portdata->in_urbs[i];
599                if (!urb)
600                        continue;
601                if (urb->dev != serial->dev) {
602                        dev_dbg(&port->dev, "%s: dev %p != %p",
603                                 __func__, urb->dev, serial->dev);
604                        continue;
605                }
606
607                /*
608                 * make sure endpoint data toggle is synchronized with the
609                 * device
610                 */
611                usb_clear_halt(urb->dev, urb->pipe);
612
613                result = usb_submit_urb(urb, GFP_KERNEL);
614                if (result) {
615                        dev_err(&port->dev, "submit urb %d failed (%d) %d\n",
616                                i, result, urb->transfer_buffer_length);
617                }
618        }
619
620        port->tty->low_latency = 1;
621
622        sierra_send_setup(port);
623
624        /* start up the interrupt endpoint if we have one */
625        if (port->interrupt_in_urb) {
626                result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
627                if (result)
628                        dev_err(&port->dev, "submit irq_in urb failed %d\n",
629                                result);
630        }
631        return 0;
632}
633
634static void sierra_close(struct usb_serial_port *port, struct file *filp)
635{
636        int i;
637        struct usb_serial *serial = port->serial;
638        struct sierra_port_private *portdata;
639
640        dev_dbg(&port->dev, "%s", __func__);
641        portdata = usb_get_serial_port_data(port);
642
643        portdata->rts_state = 0;
644        portdata->dtr_state = 0;
645
646        if (serial->dev) {
647                sierra_send_setup(port);
648
649                /* Stop reading/writing urbs */
650                for (i = 0; i < N_IN_URB; i++)
651                        usb_kill_urb(portdata->in_urbs[i]);
652        }
653
654        usb_kill_urb(port->interrupt_in_urb);
655
656        port->tty = NULL;
657}
658
659static int sierra_startup(struct usb_serial *serial)
660{
661        struct usb_serial_port *port;
662        struct sierra_port_private *portdata;
663        struct urb *urb;
664        int i;
665        int j;
666
667        dev_dbg(&serial->dev->dev, "%s", __func__);
668
669        /* Set Device mode to D0 */
670        sierra_set_power_state(serial->dev, 0x0000);
671
672        /* Check NMEA and set */
673        if (nmea)
674                sierra_vsc_set_nmea(serial->dev, 1);
675
676        /* Now setup per port private data */
677        for (i = 0; i < serial->num_ports; i++) {
678                port = serial->port[i];
679                portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
680                if (!portdata) {
681                        dev_dbg(&port->dev, "%s: kmalloc for "
682                                "sierra_port_private (%d) failed!.",
683                                __func__, i);
684                        return -ENOMEM;
685                }
686                spin_lock_init(&portdata->lock);
687                for (j = 0; j < N_IN_URB; j++) {
688                        portdata->in_buffer[j] = kmalloc(IN_BUFLEN, GFP_KERNEL);
689                        if (!portdata->in_buffer[j]) {
690                                for (--j; j >= 0; j--)
691                                        kfree(portdata->in_buffer[j]);
692                                kfree(portdata);
693                                return -ENOMEM;
694                        }
695                }
696
697                usb_set_serial_port_data(port, portdata);
698
699                /* initialize the in urbs */
700                for (j = 0; j < N_IN_URB; ++j) {
701                        urb = usb_alloc_urb(0, GFP_KERNEL);
702                        if (urb == NULL) {
703                                dev_dbg(&port->dev, "%s: alloc for in "
704                                        "port failed.", __func__);
705                                continue;
706                        }
707                        /* Fill URB using supplied data. */
708                        usb_fill_bulk_urb(urb, serial->dev,
709                                          usb_rcvbulkpipe(serial->dev,
710                                                port->bulk_in_endpointAddress),
711                                          portdata->in_buffer[j], IN_BUFLEN,
712                                          sierra_indat_callback, port);
713                        portdata->in_urbs[j] = urb;
714                }
715        }
716
717        return 0;
718}
719
720static void sierra_shutdown(struct usb_serial *serial)
721{
722        int i, j;
723        struct usb_serial_port *port;
724        struct sierra_port_private *portdata;
725
726        dev_dbg(&serial->dev->dev, "%s", __func__);
727
728        for (i = 0; i < serial->num_ports; ++i) {
729                port = serial->port[i];
730                if (!port)
731                        continue;
732                portdata = usb_get_serial_port_data(port);
733                if (!portdata)
734                        continue;
735
736                for (j = 0; j < N_IN_URB; j++) {
737                        usb_kill_urb(portdata->in_urbs[j]);
738                        usb_free_urb(portdata->in_urbs[j]);
739                        kfree(portdata->in_buffer[j]);
740                }
741                kfree(portdata);
742                usb_set_serial_port_data(port, NULL);
743        }
744}
745
746static struct usb_serial_driver sierra_device = {
747        .driver = {
748                .owner =        THIS_MODULE,
749                .name =         "sierra",
750        },
751        .description       = "Sierra USB modem",
752        .id_table          = id_table,
753        .usb_driver        = &sierra_driver,
754        .num_interrupt_in  = NUM_DONT_CARE,
755        .num_bulk_in       = NUM_DONT_CARE,
756        .num_bulk_out      = NUM_DONT_CARE,
757        .calc_num_ports    = sierra_calc_num_ports,
758        .probe             = sierra_probe,
759        .open              = sierra_open,
760        .close             = sierra_close,
761        .write             = sierra_write,
762        .write_room        = sierra_write_room,
763        .set_termios       = sierra_set_termios,
764        .tiocmget          = sierra_tiocmget,
765        .tiocmset          = sierra_tiocmset,
766        .attach            = sierra_startup,
767        .shutdown          = sierra_shutdown,
768        .read_int_callback = sierra_instat_callback,
769};
770
771/* Functions used by new usb-serial code. */
772static int __init sierra_init(void)
773{
774        int retval;
775        retval = usb_serial_register(&sierra_device);
776        if (retval)
777                goto failed_device_register;
778
779
780        retval = usb_register(&sierra_driver);
781        if (retval)
782                goto failed_driver_register;
783
784        info(DRIVER_DESC ": " DRIVER_VERSION);
785
786        return 0;
787
788failed_driver_register:
789        usb_serial_deregister(&sierra_device);
790failed_device_register:
791        return retval;
792}
793
794static void __exit sierra_exit(void)
795{
796        usb_deregister(&sierra_driver);
797        usb_serial_deregister(&sierra_device);
798}
799
800module_init(sierra_init);
801module_exit(sierra_exit);
802
803MODULE_AUTHOR(DRIVER_AUTHOR);
804MODULE_DESCRIPTION(DRIVER_DESC);
805MODULE_VERSION(DRIVER_VERSION);
806MODULE_LICENSE("GPL");
807
808module_param(truinstall, bool, S_IRUGO | S_IWUSR);
809MODULE_PARM_DESC(truinstall, "TRU-Install support");
810
811module_param(nmea, bool, S_IRUGO | S_IWUSR);
812MODULE_PARM_DESC(nmea, "NMEA streaming");
813
814module_param(debug, bool, S_IRUGO | S_IWUSR);
815MODULE_PARM_DESC(debug, "Debug messages");
Note: See TracBrowser for help on using the repository browser.