source: src/linux/sl2312/linux-2.6.23/drivers/usb/serial/sierra.c @ 11897

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

3g/umts/cdma support for wbd-111

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