PageRenderTime 72ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/isdn/gigaset/bas-gigaset.c

https://bitbucket.org/ndreys/linux-sunxi
C | 2657 lines | 1921 code | 275 blank | 461 comment | 267 complexity | 3983f5f734c9ecaf9102d8ce2f8cdbd4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * USB driver for Gigaset 307x base via direct USB connection.
  3. *
  4. * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
  5. * Tilman Schmidt <tilman@imap.cc>,
  6. * Stefan Eilers.
  7. *
  8. * =====================================================================
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. * =====================================================================
  14. */
  15. #include "gigaset.h"
  16. #include <linux/usb.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  21. #define DRIVER_DESC "USB Driver for Gigaset 307x"
  22. /* Module parameters */
  23. static int startmode = SM_ISDN;
  24. static int cidmode = 1;
  25. module_param(startmode, int, S_IRUGO);
  26. module_param(cidmode, int, S_IRUGO);
  27. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  28. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  29. #define GIGASET_MINORS 1
  30. #define GIGASET_MINOR 16
  31. #define GIGASET_MODULENAME "bas_gigaset"
  32. #define GIGASET_DEVNAME "ttyGB"
  33. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  34. #define IF_WRITEBUF 264
  35. /* interrupt pipe message size according to ibid. ch. 2.2 */
  36. #define IP_MSGSIZE 3
  37. /* Values for the Gigaset 307x */
  38. #define USB_GIGA_VENDOR_ID 0x0681
  39. #define USB_3070_PRODUCT_ID 0x0001
  40. #define USB_3075_PRODUCT_ID 0x0002
  41. #define USB_SX303_PRODUCT_ID 0x0021
  42. #define USB_SX353_PRODUCT_ID 0x0022
  43. /* table of devices that work with this driver */
  44. static const struct usb_device_id gigaset_table[] = {
  45. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
  46. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
  47. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
  48. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
  49. { } /* Terminating entry */
  50. };
  51. MODULE_DEVICE_TABLE(usb, gigaset_table);
  52. /*======================= local function prototypes ==========================*/
  53. /* function called if a new device belonging to this driver is connected */
  54. static int gigaset_probe(struct usb_interface *interface,
  55. const struct usb_device_id *id);
  56. /* Function will be called if the device is unplugged */
  57. static void gigaset_disconnect(struct usb_interface *interface);
  58. /* functions called before/after suspend */
  59. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
  60. static int gigaset_resume(struct usb_interface *intf);
  61. /* functions called before/after device reset */
  62. static int gigaset_pre_reset(struct usb_interface *intf);
  63. static int gigaset_post_reset(struct usb_interface *intf);
  64. static int atread_submit(struct cardstate *, int);
  65. static void stopurbs(struct bas_bc_state *);
  66. static int req_submit(struct bc_state *, int, int, int);
  67. static int atwrite_submit(struct cardstate *, unsigned char *, int);
  68. static int start_cbsend(struct cardstate *);
  69. /*============================================================================*/
  70. struct bas_cardstate {
  71. struct usb_device *udev; /* USB device pointer */
  72. struct usb_interface *interface; /* interface for this device */
  73. unsigned char minor; /* starting minor number */
  74. struct urb *urb_ctrl; /* control pipe default URB */
  75. struct usb_ctrlrequest dr_ctrl;
  76. struct timer_list timer_ctrl; /* control request timeout */
  77. int retry_ctrl;
  78. struct timer_list timer_atrdy; /* AT command ready timeout */
  79. struct urb *urb_cmd_out; /* for sending AT commands */
  80. struct usb_ctrlrequest dr_cmd_out;
  81. int retry_cmd_out;
  82. struct urb *urb_cmd_in; /* for receiving AT replies */
  83. struct usb_ctrlrequest dr_cmd_in;
  84. struct timer_list timer_cmd_in; /* receive request timeout */
  85. unsigned char *rcvbuf; /* AT reply receive buffer */
  86. struct urb *urb_int_in; /* URB for interrupt pipe */
  87. unsigned char *int_in_buf;
  88. struct work_struct int_in_wq; /* for usb_clear_halt() */
  89. struct timer_list timer_int_in; /* int read retry delay */
  90. int retry_int_in;
  91. spinlock_t lock; /* locks all following */
  92. int basstate; /* bitmap (BS_*) */
  93. int pending; /* uncompleted base request */
  94. wait_queue_head_t waitqueue;
  95. int rcvbuf_size; /* size of AT receive buffer */
  96. /* 0: no receive in progress */
  97. int retry_cmd_in; /* receive req retry count */
  98. };
  99. /* status of direct USB connection to 307x base (bits in basstate) */
  100. #define BS_ATOPEN 0x001 /* AT channel open */
  101. #define BS_B1OPEN 0x002 /* B channel 1 open */
  102. #define BS_B2OPEN 0x004 /* B channel 2 open */
  103. #define BS_ATREADY 0x008 /* base ready for AT command */
  104. #define BS_INIT 0x010 /* base has signalled INIT_OK */
  105. #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
  106. #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
  107. #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
  108. #define BS_SUSPEND 0x100 /* USB port suspended */
  109. #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
  110. static struct gigaset_driver *driver;
  111. /* usb specific object needed to register this driver with the usb subsystem */
  112. static struct usb_driver gigaset_usb_driver = {
  113. .name = GIGASET_MODULENAME,
  114. .probe = gigaset_probe,
  115. .disconnect = gigaset_disconnect,
  116. .id_table = gigaset_table,
  117. .suspend = gigaset_suspend,
  118. .resume = gigaset_resume,
  119. .reset_resume = gigaset_post_reset,
  120. .pre_reset = gigaset_pre_reset,
  121. .post_reset = gigaset_post_reset,
  122. };
  123. /* get message text for usb_submit_urb return code
  124. */
  125. static char *get_usb_rcmsg(int rc)
  126. {
  127. static char unkmsg[28];
  128. switch (rc) {
  129. case 0:
  130. return "success";
  131. case -ENOMEM:
  132. return "out of memory";
  133. case -ENODEV:
  134. return "device not present";
  135. case -ENOENT:
  136. return "endpoint not present";
  137. case -ENXIO:
  138. return "URB type not supported";
  139. case -EINVAL:
  140. return "invalid argument";
  141. case -EAGAIN:
  142. return "start frame too early or too much scheduled";
  143. case -EFBIG:
  144. return "too many isoc frames requested";
  145. case -EPIPE:
  146. return "endpoint stalled";
  147. case -EMSGSIZE:
  148. return "invalid packet size";
  149. case -ENOSPC:
  150. return "would overcommit USB bandwidth";
  151. case -ESHUTDOWN:
  152. return "device shut down";
  153. case -EPERM:
  154. return "reject flag set";
  155. case -EHOSTUNREACH:
  156. return "device suspended";
  157. default:
  158. snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
  159. return unkmsg;
  160. }
  161. }
  162. /* get message text for USB status code
  163. */
  164. static char *get_usb_statmsg(int status)
  165. {
  166. static char unkmsg[28];
  167. switch (status) {
  168. case 0:
  169. return "success";
  170. case -ENOENT:
  171. return "unlinked (sync)";
  172. case -EINPROGRESS:
  173. return "URB still pending";
  174. case -EPROTO:
  175. return "bitstuff error, timeout, or unknown USB error";
  176. case -EILSEQ:
  177. return "CRC mismatch, timeout, or unknown USB error";
  178. case -ETIME:
  179. return "USB response timeout";
  180. case -EPIPE:
  181. return "endpoint stalled";
  182. case -ECOMM:
  183. return "IN buffer overrun";
  184. case -ENOSR:
  185. return "OUT buffer underrun";
  186. case -EOVERFLOW:
  187. return "endpoint babble";
  188. case -EREMOTEIO:
  189. return "short packet";
  190. case -ENODEV:
  191. return "device removed";
  192. case -EXDEV:
  193. return "partial isoc transfer";
  194. case -EINVAL:
  195. return "ISO madness";
  196. case -ECONNRESET:
  197. return "unlinked (async)";
  198. case -ESHUTDOWN:
  199. return "device shut down";
  200. default:
  201. snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
  202. return unkmsg;
  203. }
  204. }
  205. /* usb_pipetype_str
  206. * retrieve string representation of USB pipe type
  207. */
  208. static inline char *usb_pipetype_str(int pipe)
  209. {
  210. if (usb_pipeisoc(pipe))
  211. return "Isoc";
  212. if (usb_pipeint(pipe))
  213. return "Int";
  214. if (usb_pipecontrol(pipe))
  215. return "Ctrl";
  216. if (usb_pipebulk(pipe))
  217. return "Bulk";
  218. return "?";
  219. }
  220. /* dump_urb
  221. * write content of URB to syslog for debugging
  222. */
  223. static inline void dump_urb(enum debuglevel level, const char *tag,
  224. struct urb *urb)
  225. {
  226. #ifdef CONFIG_GIGASET_DEBUG
  227. int i;
  228. gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
  229. if (urb) {
  230. gig_dbg(level,
  231. " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
  232. "hcpriv=0x%08lx, transfer_flags=0x%x,",
  233. (unsigned long) urb->dev,
  234. usb_pipetype_str(urb->pipe),
  235. usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
  236. usb_pipein(urb->pipe) ? "in" : "out",
  237. (unsigned long) urb->hcpriv,
  238. urb->transfer_flags);
  239. gig_dbg(level,
  240. " transfer_buffer=0x%08lx[%d], actual_length=%d, "
  241. "setup_packet=0x%08lx,",
  242. (unsigned long) urb->transfer_buffer,
  243. urb->transfer_buffer_length, urb->actual_length,
  244. (unsigned long) urb->setup_packet);
  245. gig_dbg(level,
  246. " start_frame=%d, number_of_packets=%d, interval=%d, "
  247. "error_count=%d,",
  248. urb->start_frame, urb->number_of_packets, urb->interval,
  249. urb->error_count);
  250. gig_dbg(level,
  251. " context=0x%08lx, complete=0x%08lx, "
  252. "iso_frame_desc[]={",
  253. (unsigned long) urb->context,
  254. (unsigned long) urb->complete);
  255. for (i = 0; i < urb->number_of_packets; i++) {
  256. struct usb_iso_packet_descriptor *pifd
  257. = &urb->iso_frame_desc[i];
  258. gig_dbg(level,
  259. " {offset=%u, length=%u, actual_length=%u, "
  260. "status=%u}",
  261. pifd->offset, pifd->length, pifd->actual_length,
  262. pifd->status);
  263. }
  264. }
  265. gig_dbg(level, "}}");
  266. #endif
  267. }
  268. /* read/set modem control bits etc. (m10x only) */
  269. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  270. unsigned new_state)
  271. {
  272. return -EINVAL;
  273. }
  274. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  275. {
  276. return -EINVAL;
  277. }
  278. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  279. {
  280. return -EINVAL;
  281. }
  282. /* set/clear bits in base connection state, return previous state
  283. */
  284. static inline int update_basstate(struct bas_cardstate *ucs,
  285. int set, int clear)
  286. {
  287. unsigned long flags;
  288. int state;
  289. spin_lock_irqsave(&ucs->lock, flags);
  290. state = ucs->basstate;
  291. ucs->basstate = (state & ~clear) | set;
  292. spin_unlock_irqrestore(&ucs->lock, flags);
  293. return state;
  294. }
  295. /* error_hangup
  296. * hang up any existing connection because of an unrecoverable error
  297. * This function may be called from any context and takes care of scheduling
  298. * the necessary actions for execution outside of interrupt context.
  299. * cs->lock must not be held.
  300. * argument:
  301. * B channel control structure
  302. */
  303. static inline void error_hangup(struct bc_state *bcs)
  304. {
  305. struct cardstate *cs = bcs->cs;
  306. gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
  307. gigaset_schedule_event(cs);
  308. }
  309. /* error_reset
  310. * reset Gigaset device because of an unrecoverable error
  311. * This function may be called from any context, and takes care of
  312. * scheduling the necessary actions for execution outside of interrupt context.
  313. * cs->hw.bas->lock must not be held.
  314. * argument:
  315. * controller state structure
  316. */
  317. static inline void error_reset(struct cardstate *cs)
  318. {
  319. /* reset interrupt pipe to recover (ignore errors) */
  320. update_basstate(cs->hw.bas, BS_RESETTING, 0);
  321. if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
  322. /* submission failed, escalate to USB port reset */
  323. usb_queue_reset_device(cs->hw.bas->interface);
  324. }
  325. /* check_pending
  326. * check for completion of pending control request
  327. * parameter:
  328. * ucs hardware specific controller state structure
  329. */
  330. static void check_pending(struct bas_cardstate *ucs)
  331. {
  332. unsigned long flags;
  333. spin_lock_irqsave(&ucs->lock, flags);
  334. switch (ucs->pending) {
  335. case 0:
  336. break;
  337. case HD_OPEN_ATCHANNEL:
  338. if (ucs->basstate & BS_ATOPEN)
  339. ucs->pending = 0;
  340. break;
  341. case HD_OPEN_B1CHANNEL:
  342. if (ucs->basstate & BS_B1OPEN)
  343. ucs->pending = 0;
  344. break;
  345. case HD_OPEN_B2CHANNEL:
  346. if (ucs->basstate & BS_B2OPEN)
  347. ucs->pending = 0;
  348. break;
  349. case HD_CLOSE_ATCHANNEL:
  350. if (!(ucs->basstate & BS_ATOPEN))
  351. ucs->pending = 0;
  352. break;
  353. case HD_CLOSE_B1CHANNEL:
  354. if (!(ucs->basstate & BS_B1OPEN))
  355. ucs->pending = 0;
  356. break;
  357. case HD_CLOSE_B2CHANNEL:
  358. if (!(ucs->basstate & BS_B2OPEN))
  359. ucs->pending = 0;
  360. break;
  361. case HD_DEVICE_INIT_ACK: /* no reply expected */
  362. ucs->pending = 0;
  363. break;
  364. case HD_RESET_INTERRUPT_PIPE:
  365. if (!(ucs->basstate & BS_RESETTING))
  366. ucs->pending = 0;
  367. break;
  368. /*
  369. * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
  370. * and should never end up here
  371. */
  372. default:
  373. dev_warn(&ucs->interface->dev,
  374. "unknown pending request 0x%02x cleared\n",
  375. ucs->pending);
  376. ucs->pending = 0;
  377. }
  378. if (!ucs->pending)
  379. del_timer(&ucs->timer_ctrl);
  380. spin_unlock_irqrestore(&ucs->lock, flags);
  381. }
  382. /* cmd_in_timeout
  383. * timeout routine for command input request
  384. * argument:
  385. * controller state structure
  386. */
  387. static void cmd_in_timeout(unsigned long data)
  388. {
  389. struct cardstate *cs = (struct cardstate *) data;
  390. struct bas_cardstate *ucs = cs->hw.bas;
  391. int rc;
  392. if (!ucs->rcvbuf_size) {
  393. gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
  394. return;
  395. }
  396. if (ucs->retry_cmd_in++ >= BAS_RETRY) {
  397. dev_err(cs->dev,
  398. "control read: timeout, giving up after %d tries\n",
  399. ucs->retry_cmd_in);
  400. kfree(ucs->rcvbuf);
  401. ucs->rcvbuf = NULL;
  402. ucs->rcvbuf_size = 0;
  403. error_reset(cs);
  404. return;
  405. }
  406. gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
  407. __func__, ucs->retry_cmd_in);
  408. rc = atread_submit(cs, BAS_TIMEOUT);
  409. if (rc < 0) {
  410. kfree(ucs->rcvbuf);
  411. ucs->rcvbuf = NULL;
  412. ucs->rcvbuf_size = 0;
  413. if (rc != -ENODEV)
  414. error_reset(cs);
  415. }
  416. }
  417. /* read_ctrl_callback
  418. * USB completion handler for control pipe input
  419. * called by the USB subsystem in interrupt context
  420. * parameter:
  421. * urb USB request block
  422. * urb->context = inbuf structure for controller state
  423. */
  424. static void read_ctrl_callback(struct urb *urb)
  425. {
  426. struct inbuf_t *inbuf = urb->context;
  427. struct cardstate *cs = inbuf->cs;
  428. struct bas_cardstate *ucs = cs->hw.bas;
  429. int status = urb->status;
  430. unsigned numbytes;
  431. int rc;
  432. update_basstate(ucs, 0, BS_ATRDPEND);
  433. wake_up(&ucs->waitqueue);
  434. del_timer(&ucs->timer_cmd_in);
  435. switch (status) {
  436. case 0: /* normal completion */
  437. numbytes = urb->actual_length;
  438. if (unlikely(numbytes != ucs->rcvbuf_size)) {
  439. dev_warn(cs->dev,
  440. "control read: received %d chars, expected %d\n",
  441. numbytes, ucs->rcvbuf_size);
  442. if (numbytes > ucs->rcvbuf_size)
  443. numbytes = ucs->rcvbuf_size;
  444. }
  445. /* copy received bytes to inbuf, notify event layer */
  446. if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
  447. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  448. gigaset_schedule_event(cs);
  449. }
  450. break;
  451. case -ENOENT: /* cancelled */
  452. case -ECONNRESET: /* cancelled (async) */
  453. case -EINPROGRESS: /* pending */
  454. case -ENODEV: /* device removed */
  455. case -ESHUTDOWN: /* device shut down */
  456. /* no further action necessary */
  457. gig_dbg(DEBUG_USBREQ, "%s: %s",
  458. __func__, get_usb_statmsg(status));
  459. break;
  460. default: /* other errors: retry */
  461. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  462. gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
  463. get_usb_statmsg(status), ucs->retry_cmd_in);
  464. rc = atread_submit(cs, BAS_TIMEOUT);
  465. if (rc >= 0)
  466. /* successfully resubmitted, skip freeing */
  467. return;
  468. if (rc == -ENODEV)
  469. /* disconnect, no further action necessary */
  470. break;
  471. }
  472. dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
  473. get_usb_statmsg(status), ucs->retry_cmd_in);
  474. error_reset(cs);
  475. }
  476. /* read finished, free buffer */
  477. kfree(ucs->rcvbuf);
  478. ucs->rcvbuf = NULL;
  479. ucs->rcvbuf_size = 0;
  480. }
  481. /* atread_submit
  482. * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
  483. * parameters:
  484. * cs controller state structure
  485. * timeout timeout in 1/10 sec., 0: none
  486. * return value:
  487. * 0 on success
  488. * -EBUSY if another request is pending
  489. * any URB submission error code
  490. */
  491. static int atread_submit(struct cardstate *cs, int timeout)
  492. {
  493. struct bas_cardstate *ucs = cs->hw.bas;
  494. int basstate;
  495. int ret;
  496. gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
  497. ucs->rcvbuf_size);
  498. basstate = update_basstate(ucs, BS_ATRDPEND, 0);
  499. if (basstate & BS_ATRDPEND) {
  500. dev_err(cs->dev,
  501. "could not submit HD_READ_ATMESSAGE: URB busy\n");
  502. return -EBUSY;
  503. }
  504. if (basstate & BS_SUSPEND) {
  505. dev_notice(cs->dev,
  506. "HD_READ_ATMESSAGE not submitted, "
  507. "suspend in progress\n");
  508. update_basstate(ucs, 0, BS_ATRDPEND);
  509. /* treat like disconnect */
  510. return -ENODEV;
  511. }
  512. ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
  513. ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
  514. ucs->dr_cmd_in.wValue = 0;
  515. ucs->dr_cmd_in.wIndex = 0;
  516. ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
  517. usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
  518. usb_rcvctrlpipe(ucs->udev, 0),
  519. (unsigned char *) &ucs->dr_cmd_in,
  520. ucs->rcvbuf, ucs->rcvbuf_size,
  521. read_ctrl_callback, cs->inbuf);
  522. ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
  523. if (ret != 0) {
  524. update_basstate(ucs, 0, BS_ATRDPEND);
  525. dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
  526. get_usb_rcmsg(ret));
  527. return ret;
  528. }
  529. if (timeout > 0) {
  530. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  531. mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
  532. }
  533. return 0;
  534. }
  535. /* int_in_work
  536. * workqueue routine to clear halt on interrupt in endpoint
  537. */
  538. static void int_in_work(struct work_struct *work)
  539. {
  540. struct bas_cardstate *ucs =
  541. container_of(work, struct bas_cardstate, int_in_wq);
  542. struct urb *urb = ucs->urb_int_in;
  543. struct cardstate *cs = urb->context;
  544. int rc;
  545. /* clear halt condition */
  546. rc = usb_clear_halt(ucs->udev, urb->pipe);
  547. gig_dbg(DEBUG_USBREQ, "clear_halt: %s", get_usb_rcmsg(rc));
  548. if (rc == 0)
  549. /* success, resubmit interrupt read URB */
  550. rc = usb_submit_urb(urb, GFP_ATOMIC);
  551. if (rc != 0 && rc != -ENODEV) {
  552. dev_err(cs->dev, "clear halt failed: %s\n", get_usb_rcmsg(rc));
  553. rc = usb_lock_device_for_reset(ucs->udev, ucs->interface);
  554. if (rc == 0) {
  555. rc = usb_reset_device(ucs->udev);
  556. usb_unlock_device(ucs->udev);
  557. }
  558. }
  559. ucs->retry_int_in = 0;
  560. }
  561. /* int_in_resubmit
  562. * timer routine for interrupt read delayed resubmit
  563. * argument:
  564. * controller state structure
  565. */
  566. static void int_in_resubmit(unsigned long data)
  567. {
  568. struct cardstate *cs = (struct cardstate *) data;
  569. struct bas_cardstate *ucs = cs->hw.bas;
  570. int rc;
  571. if (ucs->retry_int_in++ >= BAS_RETRY) {
  572. dev_err(cs->dev, "interrupt read: giving up after %d tries\n",
  573. ucs->retry_int_in);
  574. usb_queue_reset_device(ucs->interface);
  575. return;
  576. }
  577. gig_dbg(DEBUG_USBREQ, "%s: retry %d", __func__, ucs->retry_int_in);
  578. rc = usb_submit_urb(ucs->urb_int_in, GFP_ATOMIC);
  579. if (rc != 0 && rc != -ENODEV) {
  580. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  581. get_usb_rcmsg(rc));
  582. usb_queue_reset_device(ucs->interface);
  583. }
  584. }
  585. /* read_int_callback
  586. * USB completion handler for interrupt pipe input
  587. * called by the USB subsystem in interrupt context
  588. * parameter:
  589. * urb USB request block
  590. * urb->context = controller state structure
  591. */
  592. static void read_int_callback(struct urb *urb)
  593. {
  594. struct cardstate *cs = urb->context;
  595. struct bas_cardstate *ucs = cs->hw.bas;
  596. struct bc_state *bcs;
  597. int status = urb->status;
  598. unsigned long flags;
  599. int rc;
  600. unsigned l;
  601. int channel;
  602. switch (status) {
  603. case 0: /* success */
  604. ucs->retry_int_in = 0;
  605. break;
  606. case -EPIPE: /* endpoint stalled */
  607. schedule_work(&ucs->int_in_wq);
  608. /* fall through */
  609. case -ENOENT: /* cancelled */
  610. case -ECONNRESET: /* cancelled (async) */
  611. case -EINPROGRESS: /* pending */
  612. case -ENODEV: /* device removed */
  613. case -ESHUTDOWN: /* device shut down */
  614. /* no further action necessary */
  615. gig_dbg(DEBUG_USBREQ, "%s: %s",
  616. __func__, get_usb_statmsg(status));
  617. return;
  618. case -EPROTO: /* protocol error or unplug */
  619. case -EILSEQ:
  620. case -ETIME:
  621. /* resubmit after delay */
  622. gig_dbg(DEBUG_USBREQ, "%s: %s",
  623. __func__, get_usb_statmsg(status));
  624. mod_timer(&ucs->timer_int_in, jiffies + HZ / 10);
  625. return;
  626. default: /* other errors: just resubmit */
  627. dev_warn(cs->dev, "interrupt read: %s\n",
  628. get_usb_statmsg(status));
  629. goto resubmit;
  630. }
  631. /* drop incomplete packets even if the missing bytes wouldn't matter */
  632. if (unlikely(urb->actual_length < IP_MSGSIZE)) {
  633. dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
  634. urb->actual_length);
  635. goto resubmit;
  636. }
  637. l = (unsigned) ucs->int_in_buf[1] +
  638. (((unsigned) ucs->int_in_buf[2]) << 8);
  639. gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
  640. urb->actual_length, (int)ucs->int_in_buf[0], l,
  641. (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
  642. channel = 0;
  643. switch (ucs->int_in_buf[0]) {
  644. case HD_DEVICE_INIT_OK:
  645. update_basstate(ucs, BS_INIT, 0);
  646. break;
  647. case HD_READY_SEND_ATDATA:
  648. del_timer(&ucs->timer_atrdy);
  649. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  650. start_cbsend(cs);
  651. break;
  652. case HD_OPEN_B2CHANNEL_ACK:
  653. ++channel;
  654. case HD_OPEN_B1CHANNEL_ACK:
  655. bcs = cs->bcs + channel;
  656. update_basstate(ucs, BS_B1OPEN << channel, 0);
  657. gigaset_bchannel_up(bcs);
  658. break;
  659. case HD_OPEN_ATCHANNEL_ACK:
  660. update_basstate(ucs, BS_ATOPEN, 0);
  661. start_cbsend(cs);
  662. break;
  663. case HD_CLOSE_B2CHANNEL_ACK:
  664. ++channel;
  665. case HD_CLOSE_B1CHANNEL_ACK:
  666. bcs = cs->bcs + channel;
  667. update_basstate(ucs, 0, BS_B1OPEN << channel);
  668. stopurbs(bcs->hw.bas);
  669. gigaset_bchannel_down(bcs);
  670. break;
  671. case HD_CLOSE_ATCHANNEL_ACK:
  672. update_basstate(ucs, 0, BS_ATOPEN);
  673. break;
  674. case HD_B2_FLOW_CONTROL:
  675. ++channel;
  676. case HD_B1_FLOW_CONTROL:
  677. bcs = cs->bcs + channel;
  678. atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
  679. &bcs->hw.bas->corrbytes);
  680. gig_dbg(DEBUG_ISO,
  681. "Flow control (channel %d, sub %d): 0x%02x => %d",
  682. channel, bcs->hw.bas->numsub, l,
  683. atomic_read(&bcs->hw.bas->corrbytes));
  684. break;
  685. case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
  686. if (!l) {
  687. dev_warn(cs->dev,
  688. "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
  689. break;
  690. }
  691. spin_lock_irqsave(&cs->lock, flags);
  692. if (ucs->basstate & BS_ATRDPEND) {
  693. spin_unlock_irqrestore(&cs->lock, flags);
  694. dev_warn(cs->dev,
  695. "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
  696. l, ucs->rcvbuf_size);
  697. break;
  698. }
  699. if (ucs->rcvbuf_size) {
  700. /* throw away previous buffer - we have no queue */
  701. dev_err(cs->dev,
  702. "receive AT data overrun, %d bytes lost\n",
  703. ucs->rcvbuf_size);
  704. kfree(ucs->rcvbuf);
  705. ucs->rcvbuf_size = 0;
  706. }
  707. ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
  708. if (ucs->rcvbuf == NULL) {
  709. spin_unlock_irqrestore(&cs->lock, flags);
  710. dev_err(cs->dev, "out of memory receiving AT data\n");
  711. break;
  712. }
  713. ucs->rcvbuf_size = l;
  714. ucs->retry_cmd_in = 0;
  715. rc = atread_submit(cs, BAS_TIMEOUT);
  716. if (rc < 0) {
  717. kfree(ucs->rcvbuf);
  718. ucs->rcvbuf = NULL;
  719. ucs->rcvbuf_size = 0;
  720. }
  721. spin_unlock_irqrestore(&cs->lock, flags);
  722. if (rc < 0 && rc != -ENODEV)
  723. error_reset(cs);
  724. break;
  725. case HD_RESET_INTERRUPT_PIPE_ACK:
  726. update_basstate(ucs, 0, BS_RESETTING);
  727. dev_notice(cs->dev, "interrupt pipe reset\n");
  728. break;
  729. case HD_SUSPEND_END:
  730. gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
  731. break;
  732. default:
  733. dev_warn(cs->dev,
  734. "unknown Gigaset signal 0x%02x (%u) ignored\n",
  735. (int) ucs->int_in_buf[0], l);
  736. }
  737. check_pending(ucs);
  738. wake_up(&ucs->waitqueue);
  739. resubmit:
  740. rc = usb_submit_urb(urb, GFP_ATOMIC);
  741. if (unlikely(rc != 0 && rc != -ENODEV)) {
  742. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  743. get_usb_rcmsg(rc));
  744. error_reset(cs);
  745. }
  746. }
  747. /* read_iso_callback
  748. * USB completion handler for B channel isochronous input
  749. * called by the USB subsystem in interrupt context
  750. * parameter:
  751. * urb USB request block of completed request
  752. * urb->context = bc_state structure
  753. */
  754. static void read_iso_callback(struct urb *urb)
  755. {
  756. struct bc_state *bcs;
  757. struct bas_bc_state *ubc;
  758. int status = urb->status;
  759. unsigned long flags;
  760. int i, rc;
  761. /* status codes not worth bothering the tasklet with */
  762. if (unlikely(status == -ENOENT ||
  763. status == -ECONNRESET ||
  764. status == -EINPROGRESS ||
  765. status == -ENODEV ||
  766. status == -ESHUTDOWN)) {
  767. gig_dbg(DEBUG_ISO, "%s: %s",
  768. __func__, get_usb_statmsg(status));
  769. return;
  770. }
  771. bcs = urb->context;
  772. ubc = bcs->hw.bas;
  773. spin_lock_irqsave(&ubc->isoinlock, flags);
  774. if (likely(ubc->isoindone == NULL)) {
  775. /* pass URB to tasklet */
  776. ubc->isoindone = urb;
  777. ubc->isoinstatus = status;
  778. tasklet_hi_schedule(&ubc->rcvd_tasklet);
  779. } else {
  780. /* tasklet still busy, drop data and resubmit URB */
  781. gig_dbg(DEBUG_ISO, "%s: overrun", __func__);
  782. ubc->loststatus = status;
  783. for (i = 0; i < BAS_NUMFRAMES; i++) {
  784. ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
  785. if (unlikely(urb->iso_frame_desc[i].status != 0 &&
  786. urb->iso_frame_desc[i].status !=
  787. -EINPROGRESS))
  788. ubc->loststatus = urb->iso_frame_desc[i].status;
  789. urb->iso_frame_desc[i].status = 0;
  790. urb->iso_frame_desc[i].actual_length = 0;
  791. }
  792. if (likely(ubc->running)) {
  793. /* urb->dev is clobbered by USB subsystem */
  794. urb->dev = bcs->cs->hw.bas->udev;
  795. urb->transfer_flags = URB_ISO_ASAP;
  796. urb->number_of_packets = BAS_NUMFRAMES;
  797. rc = usb_submit_urb(urb, GFP_ATOMIC);
  798. if (unlikely(rc != 0 && rc != -ENODEV)) {
  799. dev_err(bcs->cs->dev,
  800. "could not resubmit isoc read URB: %s\n",
  801. get_usb_rcmsg(rc));
  802. dump_urb(DEBUG_ISO, "isoc read", urb);
  803. error_hangup(bcs);
  804. }
  805. }
  806. }
  807. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  808. }
  809. /* write_iso_callback
  810. * USB completion handler for B channel isochronous output
  811. * called by the USB subsystem in interrupt context
  812. * parameter:
  813. * urb USB request block of completed request
  814. * urb->context = isow_urbctx_t structure
  815. */
  816. static void write_iso_callback(struct urb *urb)
  817. {
  818. struct isow_urbctx_t *ucx;
  819. struct bas_bc_state *ubc;
  820. int status = urb->status;
  821. unsigned long flags;
  822. /* status codes not worth bothering the tasklet with */
  823. if (unlikely(status == -ENOENT ||
  824. status == -ECONNRESET ||
  825. status == -EINPROGRESS ||
  826. status == -ENODEV ||
  827. status == -ESHUTDOWN)) {
  828. gig_dbg(DEBUG_ISO, "%s: %s",
  829. __func__, get_usb_statmsg(status));
  830. return;
  831. }
  832. /* pass URB context to tasklet */
  833. ucx = urb->context;
  834. ubc = ucx->bcs->hw.bas;
  835. ucx->status = status;
  836. spin_lock_irqsave(&ubc->isooutlock, flags);
  837. ubc->isooutovfl = ubc->isooutdone;
  838. ubc->isooutdone = ucx;
  839. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  840. tasklet_hi_schedule(&ubc->sent_tasklet);
  841. }
  842. /* starturbs
  843. * prepare and submit USB request blocks for isochronous input and output
  844. * argument:
  845. * B channel control structure
  846. * return value:
  847. * 0 on success
  848. * < 0 on error (no URBs submitted)
  849. */
  850. static int starturbs(struct bc_state *bcs)
  851. {
  852. struct bas_bc_state *ubc = bcs->hw.bas;
  853. struct urb *urb;
  854. int j, k;
  855. int rc;
  856. /* initialize L2 reception */
  857. if (bcs->proto2 == L2_HDLC)
  858. bcs->inputstate |= INS_flag_hunt;
  859. /* submit all isochronous input URBs */
  860. ubc->running = 1;
  861. for (k = 0; k < BAS_INURBS; k++) {
  862. urb = ubc->isoinurbs[k];
  863. if (!urb) {
  864. rc = -EFAULT;
  865. goto error;
  866. }
  867. urb->dev = bcs->cs->hw.bas->udev;
  868. urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
  869. urb->transfer_flags = URB_ISO_ASAP;
  870. urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
  871. urb->transfer_buffer_length = BAS_INBUFSIZE;
  872. urb->number_of_packets = BAS_NUMFRAMES;
  873. urb->interval = BAS_FRAMETIME;
  874. urb->complete = read_iso_callback;
  875. urb->context = bcs;
  876. for (j = 0; j < BAS_NUMFRAMES; j++) {
  877. urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
  878. urb->iso_frame_desc[j].length = BAS_MAXFRAME;
  879. urb->iso_frame_desc[j].status = 0;
  880. urb->iso_frame_desc[j].actual_length = 0;
  881. }
  882. dump_urb(DEBUG_ISO, "Initial isoc read", urb);
  883. rc = usb_submit_urb(urb, GFP_ATOMIC);
  884. if (rc != 0)
  885. goto error;
  886. }
  887. /* initialize L2 transmission */
  888. gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
  889. /* set up isochronous output URBs for flag idling */
  890. for (k = 0; k < BAS_OUTURBS; ++k) {
  891. urb = ubc->isoouturbs[k].urb;
  892. if (!urb) {
  893. rc = -EFAULT;
  894. goto error;
  895. }
  896. urb->dev = bcs->cs->hw.bas->udev;
  897. urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
  898. urb->transfer_flags = URB_ISO_ASAP;
  899. urb->transfer_buffer = ubc->isooutbuf->data;
  900. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  901. urb->number_of_packets = BAS_NUMFRAMES;
  902. urb->interval = BAS_FRAMETIME;
  903. urb->complete = write_iso_callback;
  904. urb->context = &ubc->isoouturbs[k];
  905. for (j = 0; j < BAS_NUMFRAMES; ++j) {
  906. urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
  907. urb->iso_frame_desc[j].length = BAS_NORMFRAME;
  908. urb->iso_frame_desc[j].status = 0;
  909. urb->iso_frame_desc[j].actual_length = 0;
  910. }
  911. ubc->isoouturbs[k].limit = -1;
  912. }
  913. /* keep one URB free, submit the others */
  914. for (k = 0; k < BAS_OUTURBS-1; ++k) {
  915. dump_urb(DEBUG_ISO, "Initial isoc write", urb);
  916. rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
  917. if (rc != 0)
  918. goto error;
  919. }
  920. dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
  921. ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
  922. ubc->isooutdone = ubc->isooutovfl = NULL;
  923. return 0;
  924. error:
  925. stopurbs(ubc);
  926. return rc;
  927. }
  928. /* stopurbs
  929. * cancel the USB request blocks for isochronous input and output
  930. * errors are silently ignored
  931. * argument:
  932. * B channel control structure
  933. */
  934. static void stopurbs(struct bas_bc_state *ubc)
  935. {
  936. int k, rc;
  937. ubc->running = 0;
  938. for (k = 0; k < BAS_INURBS; ++k) {
  939. rc = usb_unlink_urb(ubc->isoinurbs[k]);
  940. gig_dbg(DEBUG_ISO,
  941. "%s: isoc input URB %d unlinked, result = %s",
  942. __func__, k, get_usb_rcmsg(rc));
  943. }
  944. for (k = 0; k < BAS_OUTURBS; ++k) {
  945. rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
  946. gig_dbg(DEBUG_ISO,
  947. "%s: isoc output URB %d unlinked, result = %s",
  948. __func__, k, get_usb_rcmsg(rc));
  949. }
  950. }
  951. /* Isochronous Write - Bottom Half */
  952. /* =============================== */
  953. /* submit_iso_write_urb
  954. * fill and submit the next isochronous write URB
  955. * parameters:
  956. * ucx context structure containing URB
  957. * return value:
  958. * number of frames submitted in URB
  959. * 0 if URB not submitted because no data available (isooutbuf busy)
  960. * error code < 0 on error
  961. */
  962. static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
  963. {
  964. struct urb *urb = ucx->urb;
  965. struct bas_bc_state *ubc = ucx->bcs->hw.bas;
  966. struct usb_iso_packet_descriptor *ifd;
  967. int corrbytes, nframe, rc;
  968. /* urb->dev is clobbered by USB subsystem */
  969. urb->dev = ucx->bcs->cs->hw.bas->udev;
  970. urb->transfer_flags = URB_ISO_ASAP;
  971. urb->transfer_buffer = ubc->isooutbuf->data;
  972. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  973. for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
  974. ifd = &urb->iso_frame_desc[nframe];
  975. /* compute frame length according to flow control */
  976. ifd->length = BAS_NORMFRAME;
  977. corrbytes = atomic_read(&ubc->corrbytes);
  978. if (corrbytes != 0) {
  979. gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
  980. __func__, corrbytes);
  981. if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
  982. corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
  983. else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
  984. corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
  985. ifd->length += corrbytes;
  986. atomic_add(-corrbytes, &ubc->corrbytes);
  987. }
  988. /* retrieve block of data to send */
  989. rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
  990. if (rc < 0) {
  991. if (rc == -EBUSY) {
  992. gig_dbg(DEBUG_ISO,
  993. "%s: buffer busy at frame %d",
  994. __func__, nframe);
  995. /* tasklet will be restarted from
  996. gigaset_isoc_send_skb() */
  997. } else {
  998. dev_err(ucx->bcs->cs->dev,
  999. "%s: buffer error %d at frame %d\n",
  1000. __func__, rc, nframe);
  1001. return rc;
  1002. }
  1003. break;
  1004. }
  1005. ifd->offset = rc;
  1006. ucx->limit = ubc->isooutbuf->nextread;
  1007. ifd->status = 0;
  1008. ifd->actual_length = 0;
  1009. }
  1010. if (unlikely(nframe == 0))
  1011. return 0; /* no data to send */
  1012. urb->number_of_packets = nframe;
  1013. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1014. if (unlikely(rc)) {
  1015. if (rc == -ENODEV)
  1016. /* device removed - give up silently */
  1017. gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
  1018. else
  1019. dev_err(ucx->bcs->cs->dev,
  1020. "could not submit isoc write URB: %s\n",
  1021. get_usb_rcmsg(rc));
  1022. return rc;
  1023. }
  1024. ++ubc->numsub;
  1025. return nframe;
  1026. }
  1027. /* write_iso_tasklet
  1028. * tasklet scheduled when an isochronous output URB from the Gigaset device
  1029. * has completed
  1030. * parameter:
  1031. * data B channel state structure
  1032. */
  1033. static void write_iso_tasklet(unsigned long data)
  1034. {
  1035. struct bc_state *bcs = (struct bc_state *) data;
  1036. struct bas_bc_state *ubc = bcs->hw.bas;
  1037. struct cardstate *cs = bcs->cs;
  1038. struct isow_urbctx_t *done, *next, *ovfl;
  1039. struct urb *urb;
  1040. int status;
  1041. struct usb_iso_packet_descriptor *ifd;
  1042. unsigned long flags;
  1043. int i;
  1044. struct sk_buff *skb;
  1045. int len;
  1046. int rc;
  1047. /* loop while completed URBs arrive in time */
  1048. for (;;) {
  1049. if (unlikely(!(ubc->running))) {
  1050. gig_dbg(DEBUG_ISO, "%s: not running", __func__);
  1051. return;
  1052. }
  1053. /* retrieve completed URBs */
  1054. spin_lock_irqsave(&ubc->isooutlock, flags);
  1055. done = ubc->isooutdone;
  1056. ubc->isooutdone = NULL;
  1057. ovfl = ubc->isooutovfl;
  1058. ubc->isooutovfl = NULL;
  1059. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1060. if (ovfl) {
  1061. dev_err(cs->dev, "isoc write underrun\n");
  1062. error_hangup(bcs);
  1063. break;
  1064. }
  1065. if (!done)
  1066. break;
  1067. /* submit free URB if available */
  1068. spin_lock_irqsave(&ubc->isooutlock, flags);
  1069. next = ubc->isooutfree;
  1070. ubc->isooutfree = NULL;
  1071. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1072. if (next) {
  1073. rc = submit_iso_write_urb(next);
  1074. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1075. /* could not submit URB, put it back */
  1076. spin_lock_irqsave(&ubc->isooutlock, flags);
  1077. if (ubc->isooutfree == NULL) {
  1078. ubc->isooutfree = next;
  1079. next = NULL;
  1080. }
  1081. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1082. if (next) {
  1083. /* couldn't put it back */
  1084. dev_err(cs->dev,
  1085. "losing isoc write URB\n");
  1086. error_hangup(bcs);
  1087. }
  1088. }
  1089. }
  1090. /* process completed URB */
  1091. urb = done->urb;
  1092. status = done->status;
  1093. switch (status) {
  1094. case -EXDEV: /* partial completion */
  1095. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1096. __func__);
  1097. /* fall through - what's the difference anyway? */
  1098. case 0: /* normal completion */
  1099. /* inspect individual frames
  1100. * assumptions (for lack of documentation):
  1101. * - actual_length bytes of first frame in error are
  1102. * successfully sent
  1103. * - all following frames are not sent at all
  1104. */
  1105. for (i = 0; i < BAS_NUMFRAMES; i++) {
  1106. ifd = &urb->iso_frame_desc[i];
  1107. if (ifd->status ||
  1108. ifd->actual_length != ifd->length) {
  1109. dev_warn(cs->dev,
  1110. "isoc write: frame %d[%d/%d]: %s\n",
  1111. i, ifd->actual_length,
  1112. ifd->length,
  1113. get_usb_statmsg(ifd->status));
  1114. break;
  1115. }
  1116. }
  1117. break;
  1118. case -EPIPE: /* stall - probably underrun */
  1119. dev_err(cs->dev, "isoc write: stalled\n");
  1120. error_hangup(bcs);
  1121. break;
  1122. default: /* other errors */
  1123. dev_warn(cs->dev, "isoc write: %s\n",
  1124. get_usb_statmsg(status));
  1125. }
  1126. /* mark the write buffer area covered by this URB as free */
  1127. if (done->limit >= 0)
  1128. ubc->isooutbuf->read = done->limit;
  1129. /* mark URB as free */
  1130. spin_lock_irqsave(&ubc->isooutlock, flags);
  1131. next = ubc->isooutfree;
  1132. ubc->isooutfree = done;
  1133. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1134. if (next) {
  1135. /* only one URB still active - resubmit one */
  1136. rc = submit_iso_write_urb(next);
  1137. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1138. /* couldn't submit */
  1139. error_hangup(bcs);
  1140. }
  1141. }
  1142. }
  1143. /* process queued SKBs */
  1144. while ((skb = skb_dequeue(&bcs->squeue))) {
  1145. /* copy to output buffer, doing L2 encapsulation */
  1146. len = skb->len;
  1147. if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
  1148. /* insufficient buffer space, push back onto queue */
  1149. skb_queue_head(&bcs->squeue, skb);
  1150. gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
  1151. __func__, skb_queue_len(&bcs->squeue));
  1152. break;
  1153. }
  1154. skb_pull(skb, len);
  1155. gigaset_skb_sent(bcs, skb);
  1156. dev_kfree_skb_any(skb);
  1157. }
  1158. }
  1159. /* Isochronous Read - Bottom Half */
  1160. /* ============================== */
  1161. /* read_iso_tasklet
  1162. * tasklet scheduled when an isochronous input URB from the Gigaset device
  1163. * has completed
  1164. * parameter:
  1165. * data B channel state structure
  1166. */
  1167. static void read_iso_tasklet(unsigned long data)
  1168. {
  1169. struct bc_state *bcs = (struct bc_state *) data;
  1170. struct bas_bc_state *ubc = bcs->hw.bas;
  1171. struct cardstate *cs = bcs->cs;
  1172. struct urb *urb;
  1173. int status;
  1174. struct usb_iso_packet_descriptor *ifd;
  1175. char *rcvbuf;
  1176. unsigned long flags;
  1177. int totleft, numbytes, offset, frame, rc;
  1178. /* loop while more completed URBs arrive in the meantime */
  1179. for (;;) {
  1180. /* retrieve URB */
  1181. spin_lock_irqsave(&ubc->isoinlock, flags);
  1182. urb = ubc->isoindone;
  1183. if (!urb) {
  1184. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1185. return;
  1186. }
  1187. status = ubc->isoinstatus;
  1188. ubc->isoindone = NULL;
  1189. if (unlikely(ubc->loststatus != -EINPROGRESS)) {
  1190. dev_warn(cs->dev,
  1191. "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
  1192. get_usb_statmsg(ubc->loststatus),
  1193. ubc->isoinlost);
  1194. ubc->loststatus = -EINPROGRESS;
  1195. }
  1196. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1197. if (unlikely(!(ubc->running))) {
  1198. gig_dbg(DEBUG_ISO,
  1199. "%s: channel not running, "
  1200. "dropped URB with status: %s",
  1201. __func__, get_usb_statmsg(status));
  1202. return;
  1203. }
  1204. switch (status) {
  1205. case 0: /* normal completion */
  1206. break;
  1207. case -EXDEV: /* inspect individual frames
  1208. (we do that anyway) */
  1209. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1210. __func__);
  1211. break;
  1212. case -ENOENT:
  1213. case -ECONNRESET:
  1214. case -EINPROGRESS:
  1215. gig_dbg(DEBUG_ISO, "%s: %s",
  1216. __func__, get_usb_statmsg(status));
  1217. continue; /* -> skip */
  1218. case -EPIPE:
  1219. dev_err(cs->dev, "isoc read: stalled\n");
  1220. error_hangup(bcs);
  1221. continue; /* -> skip */
  1222. default: /* other error */
  1223. dev_warn(cs->dev, "isoc read: %s\n",
  1224. get_usb_statmsg(status));
  1225. goto error;
  1226. }
  1227. rcvbuf = urb->transfer_buffer;
  1228. totleft = urb->actual_length;
  1229. for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
  1230. ifd = &urb->iso_frame_desc[frame];
  1231. numbytes = ifd->actual_length;
  1232. switch (ifd->status) {
  1233. case 0: /* success */
  1234. break;
  1235. case -EPROTO: /* protocol error or unplug */
  1236. case -EILSEQ:
  1237. case -ETIME:
  1238. /* probably just disconnected, ignore */
  1239. gig_dbg(DEBUG_ISO,
  1240. "isoc read: frame %d[%d]: %s\n",
  1241. frame, numbytes,
  1242. get_usb_statmsg(ifd->status));
  1243. break;
  1244. default: /* other error */
  1245. /* report, assume transferred bytes are ok */
  1246. dev_warn(cs->dev,
  1247. "isoc read: frame %d[%d]: %s\n",
  1248. frame, numbytes,
  1249. get_usb_statmsg(ifd->status));
  1250. }
  1251. if (unlikely(numbytes > BAS_MAXFRAME))
  1252. dev_warn(cs->dev,
  1253. "isoc read: frame %d[%d]: %s\n",
  1254. frame, numbytes,
  1255. "exceeds max frame size");
  1256. if (unlikely(numbytes > totleft)) {
  1257. dev_warn(cs->dev,
  1258. "isoc read: frame %d[%d]: %s\n",
  1259. frame, numbytes,
  1260. "exceeds total transfer length");
  1261. numbytes = totleft;
  1262. }
  1263. offset = ifd->offset;
  1264. if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
  1265. dev_warn(cs->dev,
  1266. "isoc read: frame %d[%d]: %s\n",
  1267. frame, numbytes,
  1268. "exceeds end of buffer");
  1269. numbytes = BAS_INBUFSIZE - offset;
  1270. }
  1271. gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
  1272. totleft -= numbytes;
  1273. }
  1274. if (unlikely(totleft > 0))
  1275. dev_warn(cs->dev, "isoc read: %d data bytes missing\n",
  1276. totleft);
  1277. error:
  1278. /* URB processed, resubmit */
  1279. for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
  1280. urb->iso_frame_desc[frame].status = 0;
  1281. urb->iso_frame_desc[frame].actual_length = 0;
  1282. }
  1283. /* urb->dev is clobbered by USB subsystem */
  1284. urb->dev = bcs->cs->hw.bas->udev;
  1285. urb->transfer_flags = URB_ISO_ASAP;
  1286. urb->number_of_packets = BAS_NUMFRAMES;
  1287. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1288. if (unlikely(rc != 0 && rc != -ENODEV)) {
  1289. dev_err(cs->dev,
  1290. "could not resubmit isoc read URB: %s\n",
  1291. get_usb_rcmsg(rc));
  1292. dump_urb(DEBUG_ISO, "resubmit isoc read", urb);
  1293. error_hangup(bcs);
  1294. }
  1295. }
  1296. }
  1297. /* Channel Operations */
  1298. /* ================== */
  1299. /* req_timeout
  1300. * timeout routine for control output request
  1301. * argument:
  1302. * controller state structure
  1303. */
  1304. static void req_timeout(unsigned long data)
  1305. {
  1306. struct cardstate *cs = (struct cardstate *) data;
  1307. struct bas_cardstate *ucs = cs->hw.bas;
  1308. int pending;
  1309. unsigned long flags;
  1310. check_pending(ucs);
  1311. spin_lock_irqsave(&ucs->lock, flags);
  1312. pending = ucs->pending;
  1313. ucs->pending = 0;
  1314. spin_unlock_irqrestore(&ucs->lock, flags);
  1315. switch (pending) {
  1316. case 0: /* no pending request */
  1317. gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
  1318. break;
  1319. case HD_OPEN_ATCHANNEL:
  1320. dev_err(cs->dev, "timeout opening AT channel\n");
  1321. error_reset(cs);
  1322. break;
  1323. case HD_OPEN_B1CHANNEL:
  1324. dev_err(cs->dev, "timeout opening channel 1\n");
  1325. error_hangup(&cs->bcs[0]);
  1326. break;
  1327. case HD_OPEN_B2CHANNEL:
  1328. dev_err(cs->dev, "timeout opening channel 2\n");
  1329. error_hangup(&cs->bcs[1]);
  1330. break;
  1331. case HD_CLOSE_ATCHANNEL:
  1332. dev_err(cs->dev, "timeout closing AT channel\n");
  1333. error_reset(cs);
  1334. break;
  1335. case HD_CLOSE_B1CHANNEL:
  1336. dev_err(cs->dev, "timeout closing channel 1\n");
  1337. error_reset(cs);
  1338. break;
  1339. case HD_CLOSE_B2CHANNEL:
  1340. dev_err(cs->dev, "timeout closing channel 2\n");
  1341. error_reset(cs);
  1342. break;
  1343. case HD_RESET_INTERRUPT_PIPE:
  1344. /* error recovery escalation */
  1345. dev_err(cs->dev,
  1346. "reset interrupt pipe timeout, attempting USB reset\n");
  1347. usb_queue_reset_device(ucs->interface);
  1348. break;
  1349. default:
  1350. dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
  1351. pending);
  1352. }
  1353. wake_up(&ucs->waitqueue);
  1354. }
  1355. /* write_ctrl_callback
  1356. * USB completion handler for control pipe output
  1357. * called by the USB subsystem in interrupt context
  1358. * parameter:
  1359. * urb USB request block of completed request
  1360. * urb->context = hardware specific controller state structure
  1361. */
  1362. static void write_ctrl_callback(struct urb *urb)
  1363. {
  1364. struct bas_cardstate *ucs = urb->context;
  1365. int status = urb->status;
  1366. int rc;
  1367. unsigned long flags;
  1368. /* check status */
  1369. switch (status) {
  1370. case 0: /* normal completion */
  1371. spin_lock_irqsave(&ucs->lock, flags);
  1372. switch (ucs->pending) {
  1373. case HD_DEVICE_INIT_ACK: /* no reply expected */
  1374. del_timer(&ucs->timer_ctrl);
  1375. ucs->pending = 0;
  1376. break;
  1377. }
  1378. spin_unlock_irqrestore(&ucs->lock, flags);
  1379. return;
  1380. case -ENOENT: /* cancelled */
  1381. case -ECONNRESET: /* cancelled (async) */
  1382. case -EINPROGRESS: /* pending */
  1383. case -ENODEV: /* device removed */
  1384. case -ESHUTDOWN: /* device shut down */
  1385. /* ignore silently */
  1386. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1387. __func__, get_usb_statmsg(status));
  1388. break;
  1389. default: /* any failure */
  1390. /* don't retry if suspend requested */
  1391. if (++ucs->retry_ctrl > BAS_RETRY ||
  1392. (ucs->basstate & BS_SUSPEND)) {
  1393. dev_err(&ucs->interface->dev,
  1394. "control request 0x%02x failed: %s\n",
  1395. ucs->dr_ctrl.bRequest,
  1396. get_usb_statmsg(status));
  1397. break; /* give up */
  1398. }
  1399. dev_notice(&ucs->interface->dev,
  1400. "control request 0x%02x: %s, retry %d\n",
  1401. ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
  1402. ucs->retry_ctrl);
  1403. /* urb->dev is clobbered by USB subsystem */
  1404. urb->dev = ucs->udev;
  1405. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1406. if (unlikely(rc)) {
  1407. dev_err(&ucs->interface->dev,
  1408. "could not resubmit request 0x%02x: %s\n",
  1409. ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
  1410. break;
  1411. }
  1412. /* resubmitted */
  1413. return;
  1414. }
  1415. /* failed, clear pending request */
  1416. spin_lock_irqsave(&ucs->lock, flags);
  1417. del_timer(&ucs->timer_ctrl);
  1418. ucs->pending = 0;
  1419. spin_unlock_irqrestore(&ucs->lock, flags);
  1420. wake_up(&ucs->waitqueue);
  1421. }
  1422. /* req_submit
  1423. * submit a control output request without message buffer to the Gigaset base
  1424. * and optionally start a timeout
  1425. * parameters:
  1426. * bcs B channel control structure
  1427. * req control request code (HD_*)
  1428. * val control request parameter value (set to 0 if unused)
  1429. * timeout timeout in seconds (0: no timeout)
  1430. * return value:
  1431. * 0 on success
  1432. * -EBUSY if another request is pending
  1433. * any URB submission error code
  1434. */
  1435. static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
  1436. {
  1437. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1438. int ret;
  1439. unsigned long flags;
  1440. gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
  1441. spin_lock_irqsave(&ucs->lock, flags);
  1442. if (ucs->pending) {
  1443. spin_unlock_irqrestore(&ucs->lock, flags);
  1444. dev_err(bcs->cs->dev,
  1445. "submission of request 0x%02x failed: "
  1446. "request 0x%02x still pending\n",
  1447. req, ucs->pending);
  1448. return -EBUSY;
  1449. }
  1450. ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
  1451. ucs->dr_ctrl.bRequest = req;
  1452. ucs->dr_ctrl.wValue = cpu_to_le16(val);
  1453. ucs->dr_ctrl.wIndex = 0;
  1454. ucs->dr_ctrl.wLength = 0;
  1455. usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
  1456. usb_sndctrlpipe(ucs->udev, 0),
  1457. (unsigned char *) &ucs->dr_ctrl, NULL, 0,
  1458. write_ctrl_callback, ucs);
  1459. ucs->retry_ctrl = 0;
  1460. ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
  1461. if (unlikely(ret)) {
  1462. dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
  1463. req, get_usb_rcmsg(ret));
  1464. spin_unlock_irqrestore(&ucs->lock, flags);
  1465. return ret;
  1466. }
  1467. ucs->pending = req;
  1468. if (timeout > 0) {
  1469. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  1470. mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
  1471. }
  1472. spin_unlock_irqrestore(&ucs->lock, flags);
  1473. return 0;
  1474. }
  1475. /* gigaset_init_bchannel
  1476. * called by common.c to connect a B channel
  1477. * initialize isochronous I/O and tell the Gigaset base to open the channel
  1478. * argument:
  1479. * B channel control structure
  1480. * return value:
  1481. * 0 on success, error code < 0 on error
  1482. */
  1483. static int gigaset_init_bchannel(struct bc_state *bcs)
  1484. {
  1485. struct cardstate *cs = bcs->cs;
  1486. int req, ret;
  1487. unsigned long flags;
  1488. spin_lock_irqsave(&cs->lock, flags);
  1489. if (unlikely(!cs->connected)) {
  1490. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1491. spin_unlock_irqrestore(&cs->lock, flags);
  1492. return -ENODEV;
  1493. }
  1494. if (cs->hw.bas->basstate & BS_SUSPEND) {
  1495. dev_notice(cs->dev,
  1496. "not starting isoc I/O, suspend in progress\n");
  1497. spin_unlock_irqrestore(&cs->lock, flags);
  1498. return -EHOSTUNREACH;
  1499. }
  1500. ret = starturbs(bcs);
  1501. if (ret < 0) {
  1502. spin_unlock_irqrestore(&cs->lock, flags);
  1503. dev_err(cs->dev,
  1504. "could not start isoc I/O for channel B%d: %s\n",
  1505. bcs->channel + 1,
  1506. ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
  1507. if (ret != -ENODEV)
  1508. error_hangup(bcs);
  1509. return ret;
  1510. }
  1511. req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
  1512. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1513. if (ret < 0) {
  1514. dev_err(cs->dev, "could not open channel B%d\n",
  1515. bcs->channel + 1);
  1516. stopurbs(bcs->hw.bas);
  1517. }
  1518. spin_unlock_irqrestore(&cs->lock, flags);
  1519. if (ret < 0 && ret != -ENODEV)
  1520. error_hangup(bcs);
  1521. return ret;
  1522. }
  1523. /* gigaset_close_bchannel
  1524. * called by common.c to disconnect a B channel
  1525. * tell the Gigaset base to close the channel
  1526. * stopping isochronous I/O and LL notification will be done when the
  1527. * acknowledgement for the close arrives
  1528. * argument:
  1529. * B channel control structure
  1530. * return value:
  1531. * 0 on success, error code < 0 on error
  1532. */
  1533. static int gigaset_close_bchannel(struct bc_state *bcs)
  1534. {
  1535. struct cardstate *cs = bcs->cs;
  1536. int req, ret;
  1537. unsigned long flags;
  1538. spin_lock_irqsave(&cs->lock, flags);
  1539. if (unlikely(!cs->connected)) {
  1540. spin_unlock_irqrestore(&cs->lock, flags);
  1541. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1542. return -ENODEV;
  1543. }
  1544. if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
  1545. /* channel not running: just signal common.c */
  1546. spin_unlock_irqrestore(&cs->lock, flags);
  1547. gigaset_bchannel_down(bcs);
  1548. return 0;
  1549. }
  1550. /* channel running: tell device to close it */
  1551. req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
  1552. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1553. if (ret < 0)
  1554. dev_err(cs->dev, "closing channel B%d failed\n",
  1555. bcs->channel + 1);
  1556. spin_unlock_irqrestore(&cs->lock, flags);
  1557. return ret;
  1558. }
  1559. /* Device Operations */
  1560. /* ================= */
  1561. /* complete_cb
  1562. * unqueue first command buffer from queue, waking any sleepers
  1563. * must be called with cs->cmdlock held
  1564. * parameter:
  1565. * cs controller state structure
  1566. */
  1567. static void complete_cb(struct cardstate *cs)
  1568. {
  1569. struct cmdbuf_t *cb = cs->cmdbuf;
  1570. /* unqueue completed buffer */
  1571. cs->cmdbytes -= cs->curlen;
  1572. gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
  1573. cs->curlen, cs->cmdbytes);
  1574. if (cb->next != NULL) {
  1575. cs->cmdbuf = cb->next;
  1576. cs->cmdbuf->prev = NULL;
  1577. cs->curlen = cs->cmdbuf->len;
  1578. } else {
  1579. cs->cmdbuf = NULL;
  1580. cs->lastcmdbuf = NULL;
  1581. cs->curlen = 0;
  1582. }
  1583. if (cb->wake_tasklet)
  1584. tasklet_schedule(cb->wake_tasklet);
  1585. kfree(cb);
  1586. }
  1587. /* write_command_callback
  1588. * USB completion handler for AT command transmission
  1589. * called by the USB subsystem in interrupt context
  1590. * parameter:
  1591. * urb USB request block of completed request
  1592. * urb->context = controller state structure
  1593. */
  1594. static void write_command_callback(struct urb *urb)
  1595. {
  1596. struct cardstate *cs = urb->context;
  1597. struct bas_cardstate *ucs = cs->hw.bas;
  1598. int status = urb->status;
  1599. unsigned long flags;
  1600. update_basstate(ucs, 0, BS_ATWRPEND);
  1601. wake_up(&ucs->waitqueue);
  1602. /* check status */
  1603. switch (status) {
  1604. case 0: /* normal completion */
  1605. break;
  1606. case -ENOENT: /* cancelled */
  1607. case -ECONNRESET: /* cancelled (async) */
  1608. case -EINPROGRESS: /* pending */
  1609. case -ENODEV: /* device removed */
  1610. case -ESHUTDOWN: /* device shut down */
  1611. /* ignore silently */
  1612. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1613. __func__, get_usb_statmsg(status));
  1614. return;
  1615. default: /* any failure */
  1616. if (++ucs->retry_cmd_out > BAS_RETRY) {
  1617. dev_warn(cs->dev,
  1618. "command write: %s, "
  1619. "giving up after %d retries\n",
  1620. get_usb_statmsg(status),
  1621. ucs->retry_cmd_out);
  1622. break;
  1623. }
  1624. if (ucs->basstate & BS_SUSPEND) {
  1625. dev_warn(cs->dev,
  1626. "command write: %s, "
  1627. "won't retry - suspend requested\n",
  1628. get_usb_statmsg(status));
  1629. break;
  1630. }
  1631. if (cs->cmdbuf == NULL) {
  1632. dev_warn(cs->dev,
  1633. "command write: %s, "
  1634. "cannot retry - cmdbuf gone\n",
  1635. get_usb_statmsg(status));
  1636. break;
  1637. }
  1638. dev_notice(cs->dev, "command write: %s, retry %d\n",
  1639. get_usb_statmsg(status), ucs->retry_cmd_out);
  1640. if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
  1641. /* resubmitted - bypass regular exit block */
  1642. return;
  1643. /* command send failed, assume base still waiting */
  1644. update_basstate(ucs, BS_ATREADY, 0);
  1645. }
  1646. spin_lock_irqsave(&cs->cmdlock, flags);
  1647. if (cs->cmdbuf != NULL)
  1648. complete_cb(cs);
  1649. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1650. }
  1651. /* atrdy_timeout
  1652. * timeout routine for AT command transmission
  1653. * argument:
  1654. * controller state structure
  1655. */
  1656. static void atrdy_timeout(unsigned long data)
  1657. {
  1658. struct cardstate *cs = (struct cardstate *) data;
  1659. struct bas_cardstate *ucs = cs->hw.bas;
  1660. dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
  1661. /* fake the missing signal - what else can I do? */
  1662. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  1663. start_cbsend(cs);
  1664. }
  1665. /* atwrite_submit
  1666. * submit an HD_WRITE_ATMESSAGE command URB
  1667. * parameters:
  1668. * cs controller state structure
  1669. * buf buffer containing command to send
  1670. * len length of command to send
  1671. * return value:
  1672. * 0 on success
  1673. * -EBUSY if another request is pending
  1674. * any URB submission error code
  1675. */
  1676. static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
  1677. {
  1678. struct bas_cardstate *ucs = cs->hw.bas;
  1679. int rc;
  1680. gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
  1681. if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
  1682. dev_err(cs->dev,
  1683. "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
  1684. return -EBUSY;
  1685. }
  1686. ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
  1687. ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
  1688. ucs->dr_cmd_out.wValue = 0;
  1689. ucs->dr_cmd_out.wIndex = 0;
  1690. ucs->dr_cmd_out.wLength = cpu_to_le16(len);
  1691. usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
  1692. usb_sndctrlpipe(ucs->udev, 0),
  1693. (unsigned char *) &ucs->dr_cmd_out, buf, len,
  1694. write_command_callback, cs);
  1695. rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
  1696. if (unlikely(rc)) {
  1697. update_basstate(ucs, 0, BS_ATWRPEND);
  1698. dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
  1699. get_usb_rcmsg(rc));
  1700. return rc;
  1701. }
  1702. /* submitted successfully, start timeout if necessary */
  1703. if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
  1704. gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
  1705. ATRDY_TIMEOUT);
  1706. mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
  1707. }
  1708. return 0;
  1709. }
  1710. /* start_cbsend
  1711. * start transmission of AT command queue if necessary
  1712. * parameter:
  1713. * cs controller state structure
  1714. * return value:
  1715. * 0 on success
  1716. * error code < 0 on error
  1717. */
  1718. static int start_cbsend(struct cardstate *cs)
  1719. {
  1720. struct cmdbuf_t *cb;
  1721. struct bas_cardstate *ucs = cs->hw.bas;
  1722. unsigned long flags;
  1723. int rc;
  1724. int retval = 0;
  1725. /* check if suspend requested */
  1726. if (ucs->basstate & BS_SUSPEND) {
  1727. gig_dbg(DEBUG_OUTPUT, "suspending");
  1728. return -EHOSTUNREACH;
  1729. }
  1730. /* check if AT channel is open */
  1731. if (!(ucs->basstate & BS_ATOPEN)) {
  1732. gig_dbg(DEBUG_OUTPUT, "AT channel not open");
  1733. rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
  1734. if (rc < 0) {
  1735. /* flush command queue */
  1736. spin_lock_irqsave(&cs->cmdlock, flags);
  1737. while (cs->cmdbuf != NULL)
  1738. complete_cb(cs);
  1739. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1740. }
  1741. return rc;
  1742. }
  1743. /* try to send first command in queue */
  1744. spin_lock_irqsave(&cs->cmdlock, flags);
  1745. while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
  1746. ucs->retry_cmd_out = 0;
  1747. rc = atwrite_submit(cs, cb->buf, cb->len);
  1748. if (unlikely(rc)) {
  1749. retval = rc;
  1750. complete_cb(cs);
  1751. }
  1752. }
  1753. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1754. return retval;
  1755. }
  1756. /* gigaset_write_cmd
  1757. * This function is called by the device independent part of the driver
  1758. * to transmit an AT command string to the Gigaset device.
  1759. * It encapsulates the device specific method for transmission over the
  1760. * direct USB connection to the base.
  1761. * The command string is added to the queue of commands to send, and
  1762. * USB transmission is started if necessary.
  1763. * parameters:
  1764. * cs controller state structure
  1765. * cb command buffer structure
  1766. * return value:
  1767. * number of bytes queued on success
  1768. * error code < 0 on error
  1769. */
  1770. static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
  1771. {
  1772. unsigned long flags;
  1773. int rc;
  1774. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  1775. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  1776. "CMD Transmit", cb->len, cb->buf);
  1777. /* translate "+++" escape sequence sent as a single separate command
  1778. * into "close AT channel" command for error recovery
  1779. * The next command will reopen the AT channel automatically.
  1780. */
  1781. if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
  1782. /* If an HD_RECEIVEATDATA_ACK message remains unhandled
  1783. * because of an error, the base never sends another one.
  1784. * The response channel is thus effectively blocked.
  1785. * Closing and reopening the AT channel does *not* clear
  1786. * this condition.
  1787. * As a stopgap measure, submit a zero-length AT read
  1788. * before closing the AT channel. This has the undocumented
  1789. * effect of triggering a new HD_RECEIVEATDATA_ACK message
  1790. * from the base if necessary.
  1791. * The subsequent AT channel close then discards any pending
  1792. * messages.
  1793. */
  1794. spin_lock_irqsave(&cs->lock, flags);
  1795. if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
  1796. kfree(cs->hw.bas->rcvbuf);
  1797. cs->hw.bas->rcvbuf = NULL;
  1798. cs->hw.bas->rcvbuf_size = 0;
  1799. cs->hw.bas->retry_cmd_in = 0;
  1800. atread_submit(cs, 0);
  1801. }
  1802. spin_unlock_irqrestore(&cs->lock, flags);
  1803. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
  1804. if (cb->wake_tasklet)
  1805. tasklet_schedule(cb->wake_tasklet);
  1806. if (!rc)
  1807. rc = cb->len;
  1808. kfree(cb);
  1809. return rc;
  1810. }
  1811. spin_lock_irqsave(&cs->cmdlock, flags);
  1812. cb->prev = cs->lastcmdbuf;
  1813. if (cs->lastcmdbuf)
  1814. cs->lastcmdbuf->next = cb;
  1815. else {
  1816. cs->cmdbuf = cb;
  1817. cs->curlen = cb->len;
  1818. }
  1819. cs->cmdbytes += cb->len;
  1820. cs->lastcmdbuf = cb;
  1821. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1822. spin_lock_irqsave(&cs->lock, flags);
  1823. if (unlikely(!cs->connected)) {
  1824. spin_unlock_irqrestore(&cs->lock, flags);
  1825. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1826. /* flush command queue */
  1827. spin_lock_irqsave(&cs->cmdlock, flags);
  1828. while (cs->cmdbuf != NULL)
  1829. complete_cb(cs);
  1830. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1831. return -ENODEV;
  1832. }
  1833. rc = start_cbsend(cs);
  1834. spin_unlock_irqrestore(&cs->lock, flags);
  1835. return rc < 0 ? rc : cb->len;
  1836. }
  1837. /* gigaset_write_room
  1838. * tty_driver.write_room interface routine
  1839. * return number of characters the driver will accept to be written via
  1840. * gigaset_write_cmd
  1841. * parameter:
  1842. * controller state structure
  1843. * return value:
  1844. * number of characters
  1845. */
  1846. static int gigaset_write_room(struct cardstate *cs)
  1847. {
  1848. return IF_WRITEBUF;
  1849. }
  1850. /* gigaset_chars_in_buffer
  1851. * tty_driver.chars_in_buffer interface routine
  1852. * return number of characters waiting to be sent
  1853. * parameter:
  1854. * controller state structure
  1855. * return value:
  1856. * number of characters
  1857. */
  1858. static int gigaset_chars_in_buffer(struct cardstate *cs)
  1859. {
  1860. return cs->cmdbytes;
  1861. }
  1862. /* gigaset_brkchars
  1863. * implementation of ioctl(GIGASET_BRKCHARS)
  1864. * parameter:
  1865. * controller state structure
  1866. * return value:
  1867. * -EINVAL (unimplemented function)
  1868. */
  1869. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  1870. {
  1871. return -EINVAL;
  1872. }
  1873. /* Device Initialization/Shutdown */
  1874. /* ============================== */
  1875. /* Free hardware dependent part of the B channel structure
  1876. * parameter:
  1877. * bcs B channel structure
  1878. * return value:
  1879. * !=0 on success
  1880. */
  1881. static int gigaset_freebcshw(struct bc_state *bcs)
  1882. {
  1883. struct bas_bc_state *ubc = bcs->hw.bas;
  1884. int i;
  1885. if (!ubc)
  1886. return 0;
  1887. /* kill URBs and tasklets before freeing - better safe than sorry */
  1888. ubc->running = 0;
  1889. gig_dbg(DEBUG_INIT, "%s: killing isoc URBs", __func__);
  1890. for (i = 0; i < BAS_OUTURBS; ++i) {
  1891. usb_kill_urb(ubc->isoouturbs[i].urb);
  1892. usb_free_urb(ubc->isoouturbs[i].urb);
  1893. }
  1894. for (i = 0; i < BAS_INURBS; ++i) {
  1895. usb_kill_urb(ubc->isoinurbs[i]);
  1896. usb_free_urb(ubc->isoinurbs[i]);
  1897. }
  1898. tasklet_kill(&ubc->sent_tasklet);
  1899. tasklet_kill(&ubc->rcvd_tasklet);
  1900. kfree(ubc->isooutbuf);
  1901. kfree(ubc);
  1902. bcs->hw.bas = NULL;
  1903. return 1;
  1904. }
  1905. /* Initialize hardware dependent part of the B channel structure
  1906. * parameter:
  1907. * bcs B channel structure
  1908. * return value:
  1909. * !=0 on success
  1910. */
  1911. static int gigaset_initbcshw(struct bc_state *bcs)
  1912. {
  1913. int i;
  1914. struct bas_bc_state *ubc;
  1915. bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
  1916. if (!ubc) {
  1917. pr_err("out of memory\n");
  1918. return 0;
  1919. }
  1920. ubc->running = 0;
  1921. atomic_set(&ubc->corrbytes, 0);
  1922. spin_lock_init(&ubc->isooutlock);
  1923. for (i = 0; i < BAS_OUTURBS; ++i) {
  1924. ubc->isoouturbs[i].urb = NULL;
  1925. ubc->isoouturbs[i].bcs = bcs;
  1926. }
  1927. ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
  1928. ubc->numsub = 0;
  1929. ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
  1930. if (!ubc->isooutbuf) {
  1931. pr_err("out of memory\n");
  1932. kfree(ubc);
  1933. bcs->hw.bas = NULL;
  1934. return 0;
  1935. }
  1936. tasklet_init(&ubc->sent_tasklet,
  1937. write_iso_tasklet, (unsigned long) bcs);
  1938. spin_lock_init(&ubc->isoinlock);
  1939. for (i = 0; i < BAS_INURBS; ++i)
  1940. ubc->isoinurbs[i] = NULL;
  1941. ubc->isoindone = NULL;
  1942. ubc->loststatus = -EINPROGRESS;
  1943. ubc->isoinlost = 0;
  1944. ubc->seqlen = 0;
  1945. ubc->inbyte = 0;
  1946. ubc->inbits = 0;
  1947. ubc->goodbytes = 0;
  1948. ubc->alignerrs = 0;
  1949. ubc->fcserrs = 0;
  1950. ubc->frameerrs = 0;
  1951. ubc->giants = 0;
  1952. ubc->runts = 0;
  1953. ubc->aborts = 0;
  1954. ubc->shared0s = 0;
  1955. ubc->stolen0s = 0;
  1956. tasklet_init(&ubc->rcvd_tasklet,
  1957. read_iso_tasklet, (unsigned long) bcs);
  1958. return 1;
  1959. }
  1960. static void gigaset_reinitbcshw(struct bc_state *bcs)
  1961. {
  1962. struct bas_bc_state *ubc = bcs->hw.bas;
  1963. bcs->hw.bas->running = 0;
  1964. atomic_set(&bcs->hw.bas->corrbytes, 0);
  1965. bcs->hw.bas->numsub = 0;
  1966. spin_lock_init(&ubc->isooutlock);
  1967. spin_lock_init(&ubc->isoinlock);
  1968. ubc->loststatus = -EINPROGRESS;
  1969. }
  1970. static void gigaset_freecshw(struct cardstate *cs)
  1971. {
  1972. /* timers, URBs and rcvbuf are disposed of in disconnect */
  1973. kfree(cs->hw.bas->int_in_buf);
  1974. kfree(cs->hw.bas);
  1975. cs->hw.bas = NULL;
  1976. }
  1977. static int gigaset_initcshw(struct cardstate *cs)
  1978. {
  1979. struct bas_cardstate *ucs;
  1980. cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
  1981. if (!ucs) {
  1982. pr_err("out of memory\n");
  1983. return 0;
  1984. }
  1985. ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
  1986. if (!ucs->int_in_buf) {
  1987. kfree(ucs);
  1988. pr_err("out of memory\n");
  1989. return 0;
  1990. }
  1991. ucs->urb_cmd_in = NULL;
  1992. ucs->urb_cmd_out = NULL;
  1993. ucs->rcvbuf = NULL;
  1994. ucs->rcvbuf_size = 0;
  1995. spin_lock_init(&ucs->lock);
  1996. ucs->pending = 0;
  1997. ucs->basstate = 0;
  1998. setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
  1999. setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
  2000. setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
  2001. setup_timer(&ucs->timer_int_in, int_in_resubmit, (unsigned long) cs);
  2002. init_waitqueue_head(&ucs->waitqueue);
  2003. INIT_WORK(&ucs->int_in_wq, int_in_work);
  2004. return 1;
  2005. }
  2006. /* freeurbs
  2007. * unlink and deallocate all URBs unconditionally
  2008. * caller must make sure that no commands are still in progress
  2009. * parameter:
  2010. * cs controller state structure
  2011. */
  2012. static void freeurbs(struct cardstate *cs)
  2013. {
  2014. struct bas_cardstate *ucs = cs->hw.bas;
  2015. struct bas_bc_state *ubc;
  2016. int i, j;
  2017. gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
  2018. for (j = 0; j < BAS_CHANNELS; ++j) {
  2019. ubc = cs->bcs[j].hw.bas;
  2020. for (i = 0; i < BAS_OUTURBS; ++i) {
  2021. usb_kill_urb(ubc->isoouturbs[i].urb);
  2022. usb_free_urb(ubc->isoouturbs[i].urb);
  2023. ubc->isoouturbs[i].urb = NULL;
  2024. }
  2025. for (i = 0; i < BAS_INURBS; ++i) {
  2026. usb_kill_urb(ubc->isoinurbs[i]);
  2027. usb_free_urb(ubc->isoinurbs[i]);
  2028. ubc->isoinurbs[i] = NULL;
  2029. }
  2030. }
  2031. usb_kill_urb(ucs->urb_int_in);
  2032. usb_free_urb(ucs->urb_int_in);
  2033. ucs->urb_int_in = NULL;
  2034. usb_kill_urb(ucs->urb_cmd_out);
  2035. usb_free_urb(ucs->urb_cmd_out);
  2036. ucs->urb_cmd_out = NULL;
  2037. usb_kill_urb(ucs->urb_cmd_in);
  2038. usb_free_urb(ucs->urb_cmd_in);
  2039. ucs->urb_cmd_in = NULL;
  2040. usb_kill_urb(ucs->urb_ctrl);
  2041. usb_free_urb(ucs->urb_ctrl);
  2042. ucs->urb_ctrl = NULL;
  2043. }
  2044. /* gigaset_probe
  2045. * This function is called when a new USB device is connected.
  2046. * It checks whether the new device is handled by this driver.
  2047. */
  2048. static int gigaset_probe(struct usb_interface *interface,
  2049. const struct usb_device_id *id)
  2050. {
  2051. struct usb_host_interface *hostif;
  2052. struct usb_device *udev = interface_to_usbdev(interface);
  2053. struct cardstate *cs = NULL;
  2054. struct bas_cardstate *ucs = NULL;
  2055. struct bas_bc_state *ubc;
  2056. struct usb_endpoint_descriptor *endpoint;
  2057. int i, j;
  2058. int rc;
  2059. gig_dbg(DEBUG_INIT,
  2060. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  2061. __func__, le16_to_cpu(udev->descriptor.idVendor),
  2062. le16_to_cpu(udev->descriptor.idProduct));
  2063. /* set required alternate setting */
  2064. hostif = interface->cur_altsetting;
  2065. if (hostif->desc.bAlternateSetting != 3) {
  2066. gig_dbg(DEBUG_INIT,
  2067. "%s: wrong alternate setting %d - trying to switch",
  2068. __func__, hostif->desc.bAlternateSetting);
  2069. if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
  2070. < 0) {
  2071. dev_warn(&udev->dev, "usb_set_interface failed, "
  2072. "device %d interface %d altsetting %d\n",
  2073. udev->devnum, hostif->desc.bInterfaceNumber,
  2074. hostif->desc.bAlternateSetting);
  2075. return -ENODEV;
  2076. }
  2077. hostif = interface->cur_altsetting;
  2078. }
  2079. /* Reject application specific interfaces
  2080. */
  2081. if (hostif->desc.bInterfaceClass != 255) {
  2082. dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
  2083. __func__, hostif->desc.bInterfaceClass);
  2084. return -ENODEV;
  2085. }
  2086. dev_info(&udev->dev,
  2087. "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
  2088. __func__, le16_to_cpu(udev->descriptor.idVendor),
  2089. le16_to_cpu(udev->descriptor.idProduct));
  2090. /* allocate memory for our device state and initialize it */
  2091. cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
  2092. GIGASET_MODULENAME);
  2093. if (!cs)
  2094. return -ENODEV;
  2095. ucs = cs->hw.bas;
  2096. /* save off device structure ptrs for later use */
  2097. usb_get_dev(udev);
  2098. ucs->udev = udev;
  2099. ucs->interface = interface;
  2100. cs->dev = &interface->dev;
  2101. /* allocate URBs:
  2102. * - one for the interrupt pipe
  2103. * - three for the different uses of the default control pipe
  2104. * - three for each isochronous pipe
  2105. */
  2106. if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2107. !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2108. !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
  2109. !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  2110. goto allocerr;
  2111. for (j = 0; j < BAS_CHANNELS; ++j) {
  2112. ubc = cs->bcs[j].hw.bas;
  2113. for (i = 0; i < BAS_OUTURBS; ++i)
  2114. if (!(ubc->isoouturbs[i].urb =
  2115. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2116. goto allocerr;
  2117. for (i = 0; i < BAS_INURBS; ++i)
  2118. if (!(ubc->isoinurbs[i] =
  2119. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2120. goto allocerr;
  2121. }
  2122. ucs->rcvbuf = NULL;
  2123. ucs->rcvbuf_size = 0;
  2124. /* Fill the interrupt urb and send it to the core */
  2125. endpoint = &hostif->endpoint[0].desc;
  2126. usb_fill_int_urb(ucs->urb_int_in, udev,
  2127. usb_rcvintpipe(udev,
  2128. (endpoint->bEndpointAddress) & 0x0f),
  2129. ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
  2130. endpoint->bInterval);
  2131. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2132. if (rc != 0) {
  2133. dev_err(cs->dev, "could not submit interrupt URB: %s\n",
  2134. get_usb_rcmsg(rc));
  2135. goto error;
  2136. }
  2137. ucs->retry_int_in = 0;
  2138. /* tell the device that the driver is ready */
  2139. rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
  2140. if (rc != 0)
  2141. goto error;
  2142. /* tell common part that the device is ready */
  2143. if (startmode == SM_LOCKED)
  2144. cs->mstate = MS_LOCKED;
  2145. /* save address of controller structure */
  2146. usb_set_intfdata(interface, cs);
  2147. if (!gigaset_start(cs))
  2148. goto error;
  2149. return 0;
  2150. allocerr:
  2151. dev_err(cs->dev, "could not allocate URBs\n");
  2152. error:
  2153. freeurbs(cs);
  2154. usb_set_intfdata(interface, NULL);
  2155. gigaset_freecs(cs);
  2156. return -ENODEV;
  2157. }
  2158. /* gigaset_disconnect
  2159. * This function is called when the Gigaset base is unplugged.
  2160. */
  2161. static void gigaset_disconnect(struct usb_interface *interface)
  2162. {
  2163. struct cardstate *cs;
  2164. struct bas_cardstate *ucs;
  2165. int j;
  2166. cs = usb_get_intfdata(interface);
  2167. ucs = cs->hw.bas;
  2168. dev_info(cs->dev, "disconnecting Gigaset base\n");
  2169. /* mark base as not ready, all channels disconnected */
  2170. ucs->basstate = 0;
  2171. /* tell LL all channels are down */
  2172. for (j = 0; j < BAS_CHANNELS; ++j)
  2173. gigaset_bchannel_down(cs->bcs + j);
  2174. /* stop driver (common part) */
  2175. gigaset_stop(cs);
  2176. /* stop delayed work and URBs, free ressources */
  2177. del_timer_sync(&ucs->timer_ctrl);
  2178. del_timer_sync(&ucs->timer_atrdy);
  2179. del_timer_sync(&ucs->timer_cmd_in);
  2180. del_timer_sync(&ucs->timer_int_in);
  2181. cancel_work_sync(&ucs->int_in_wq);
  2182. freeurbs(cs);
  2183. usb_set_intfdata(interface, NULL);
  2184. kfree(ucs->rcvbuf);
  2185. ucs->rcvbuf = NULL;
  2186. ucs->rcvbuf_size = 0;
  2187. usb_put_dev(ucs->udev);
  2188. ucs->interface = NULL;
  2189. ucs->udev = NULL;
  2190. cs->dev = NULL;
  2191. gigaset_freecs(cs);
  2192. }
  2193. /* gigaset_suspend
  2194. * This function is called before the USB connection is suspended.
  2195. */
  2196. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
  2197. {
  2198. struct cardstate *cs = usb_get_intfdata(intf);
  2199. struct bas_cardstate *ucs = cs->hw.bas;
  2200. int rc;
  2201. /* set suspend flag; this stops AT command/response traffic */
  2202. if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
  2203. gig_dbg(DEBUG_SUSPEND, "already suspended");
  2204. return 0;
  2205. }
  2206. /* wait a bit for blocking conditions to go away */
  2207. rc = wait_event_timeout(ucs->waitqueue,
  2208. !(ucs->basstate &
  2209. (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
  2210. BAS_TIMEOUT*HZ/10);
  2211. gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
  2212. /* check for conditions preventing suspend */
  2213. if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
  2214. dev_warn(cs->dev, "cannot suspend:\n");
  2215. if (ucs->basstate & BS_B1OPEN)
  2216. dev_warn(cs->dev, " B channel 1 open\n");
  2217. if (ucs->basstate & BS_B2OPEN)
  2218. dev_warn(cs->dev, " B channel 2 open\n");
  2219. if (ucs->basstate & BS_ATRDPEND)
  2220. dev_warn(cs->dev, " receiving AT reply\n");
  2221. if (ucs->basstate & BS_ATWRPEND)
  2222. dev_warn(cs->dev, " sending AT command\n");
  2223. update_basstate(ucs, 0, BS_SUSPEND);
  2224. return -EBUSY;
  2225. }
  2226. /* close AT channel if open */
  2227. if (ucs->basstate & BS_ATOPEN) {
  2228. gig_dbg(DEBUG_SUSPEND, "closing AT channel");
  2229. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
  2230. if (rc) {
  2231. update_basstate(ucs, 0, BS_SUSPEND);
  2232. return rc;
  2233. }
  2234. wait_event_timeout(ucs->waitqueue, !ucs->pending,
  2235. BAS_TIMEOUT*HZ/10);
  2236. /* in case of timeout, proceed anyway */
  2237. }
  2238. /* kill all URBs and delayed work that might still be pending */
  2239. usb_kill_urb(ucs->urb_ctrl);
  2240. usb_kill_urb(ucs->urb_int_in);
  2241. del_timer_sync(&ucs->timer_ctrl);
  2242. del_timer_sync(&ucs->timer_atrdy);
  2243. del_timer_sync(&ucs->timer_cmd_in);
  2244. del_timer_sync(&ucs->timer_int_in);
  2245. cancel_work_sync(&ucs->int_in_wq);
  2246. gig_dbg(DEBUG_SUSPEND, "suspend complete");
  2247. return 0;
  2248. }
  2249. /* gigaset_resume
  2250. * This function is called after the USB connection has been resumed.
  2251. */
  2252. static int gigaset_resume(struct usb_interface *intf)
  2253. {
  2254. struct cardstate *cs = usb_get_intfdata(intf);
  2255. struct bas_cardstate *ucs = cs->hw.bas;
  2256. int rc;
  2257. /* resubmit interrupt URB for spontaneous messages from base */
  2258. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2259. if (rc) {
  2260. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  2261. get_usb_rcmsg(rc));
  2262. return rc;
  2263. }
  2264. ucs->retry_int_in = 0;
  2265. /* clear suspend flag to reallow activity */
  2266. update_basstate(ucs, 0, BS_SUSPEND);
  2267. gig_dbg(DEBUG_SUSPEND, "resume complete");
  2268. return 0;
  2269. }
  2270. /* gigaset_pre_reset
  2271. * This function is called before the USB connection is reset.
  2272. */
  2273. static int gigaset_pre_reset(struct usb_interface *intf)
  2274. {
  2275. /* handle just like suspend */
  2276. return gigaset_suspend(intf, PMSG_ON);
  2277. }
  2278. /* gigaset_post_reset
  2279. * This function is called after the USB connection has been reset.
  2280. */
  2281. static int gigaset_post_reset(struct usb_interface *intf)
  2282. {
  2283. /* FIXME: send HD_DEVICE_INIT_ACK? */
  2284. /* resume operations */
  2285. return gigaset_resume(intf);
  2286. }
  2287. static const struct gigaset_ops gigops = {
  2288. gigaset_write_cmd,
  2289. gigaset_write_room,
  2290. gigaset_chars_in_buffer,
  2291. gigaset_brkchars,
  2292. gigaset_init_bchannel,
  2293. gigaset_close_bchannel,
  2294. gigaset_initbcshw,
  2295. gigaset_freebcshw,
  2296. gigaset_reinitbcshw,
  2297. gigaset_initcshw,
  2298. gigaset_freecshw,
  2299. gigaset_set_modem_ctrl,
  2300. gigaset_baud_rate,
  2301. gigaset_set_line_ctrl,
  2302. gigaset_isoc_send_skb,
  2303. gigaset_isoc_input,
  2304. };
  2305. /* bas_gigaset_init
  2306. * This function is called after the kernel module is loaded.
  2307. */
  2308. static int __init bas_gigaset_init(void)
  2309. {
  2310. int result;
  2311. /* allocate memory for our driver state and initialize it */
  2312. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  2313. GIGASET_MODULENAME, GIGASET_DEVNAME,
  2314. &gigops, THIS_MODULE);
  2315. if (driver == NULL)
  2316. goto error;
  2317. /* register this driver with the USB subsystem */
  2318. result = usb_register(&gigaset_usb_driver);
  2319. if (result < 0) {
  2320. pr_err("error %d registering USB driver\n", -result);
  2321. goto error;
  2322. }
  2323. pr_info(DRIVER_DESC "\n");
  2324. return 0;
  2325. error:
  2326. if (driver)
  2327. gigaset_freedriver(driver);
  2328. driver = NULL;
  2329. return -1;
  2330. }
  2331. /* bas_gigaset_exit
  2332. * This function is called before the kernel module is unloaded.
  2333. */
  2334. static void __exit bas_gigaset_exit(void)
  2335. {
  2336. struct bas_cardstate *ucs;
  2337. int i;
  2338. gigaset_blockdriver(driver); /* => probe will fail
  2339. * => no gigaset_start any more
  2340. */
  2341. /* stop all connected devices */
  2342. for (i = 0; i < driver->minors; i++) {
  2343. if (gigaset_shutdown(driver->cs + i) < 0)
  2344. continue; /* no device */
  2345. /* from now on, no isdn callback should be possible */
  2346. /* close all still open channels */
  2347. ucs = driver->cs[i].hw.bas;
  2348. if (ucs->basstate & BS_B1OPEN) {
  2349. gig_dbg(DEBUG_INIT, "closing B1 channel");
  2350. usb_control_msg(ucs->udev,
  2351. usb_sndctrlpipe(ucs->udev, 0),
  2352. HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
  2353. 0, 0, NULL, 0, BAS_TIMEOUT);
  2354. }
  2355. if (ucs->basstate & BS_B2OPEN) {
  2356. gig_dbg(DEBUG_INIT, "closing B2 channel");
  2357. usb_control_msg(ucs->udev,
  2358. usb_sndctrlpipe(ucs->udev, 0),
  2359. HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
  2360. 0, 0, NULL, 0, BAS_TIMEOUT);
  2361. }
  2362. if (ucs->basstate & BS_ATOPEN) {
  2363. gig_dbg(DEBUG_INIT, "closing AT channel");
  2364. usb_control_msg(ucs->udev,
  2365. usb_sndctrlpipe(ucs->udev, 0),
  2366. HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
  2367. 0, 0, NULL, 0, BAS_TIMEOUT);
  2368. }
  2369. ucs->basstate = 0;
  2370. }
  2371. /* deregister this driver with the USB subsystem */
  2372. usb_deregister(&gigaset_usb_driver);
  2373. /* this will call the disconnect-callback */
  2374. /* from now on, no disconnect/probe callback should be running */
  2375. gigaset_freedriver(driver);
  2376. driver = NULL;
  2377. }
  2378. module_init(bas_gigaset_init);
  2379. module_exit(bas_gigaset_exit);
  2380. MODULE_AUTHOR(DRIVER_AUTHOR);
  2381. MODULE_DESCRIPTION(DRIVER_DESC);
  2382. MODULE_LICENSE("GPL");