ser-gigaset.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /* This is the serial hardware link layer (HLL) for the Gigaset 307x isdn
  2. * DECT base (aka Sinus 45 isdn) using the RS232 DECT data module M101,
  3. * written as a line discipline.
  4. *
  5. * =====================================================================
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. * =====================================================================
  11. */
  12. #include "gigaset.h"
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/completion.h>
  17. /* Version Information */
  18. #define DRIVER_AUTHOR "Tilman Schmidt"
  19. #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101"
  20. #define GIGASET_MINORS 1
  21. #define GIGASET_MINOR 0
  22. #define GIGASET_MODULENAME "ser_gigaset"
  23. #define GIGASET_DEVNAME "ttyGS"
  24. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  25. #define IF_WRITEBUF 264
  26. MODULE_AUTHOR(DRIVER_AUTHOR);
  27. MODULE_DESCRIPTION(DRIVER_DESC);
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS_LDISC(N_GIGASET_M101);
  30. static int startmode = SM_ISDN;
  31. module_param(startmode, int, S_IRUGO);
  32. MODULE_PARM_DESC(startmode, "initial operation mode");
  33. static int cidmode = 1;
  34. module_param(cidmode, int, S_IRUGO);
  35. MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
  36. static struct gigaset_driver *driver;
  37. struct ser_cardstate {
  38. struct platform_device dev;
  39. struct tty_struct *tty;
  40. atomic_t refcnt;
  41. struct completion dead_cmp;
  42. };
  43. static struct platform_driver device_driver = {
  44. .driver = {
  45. .name = GIGASET_MODULENAME,
  46. },
  47. };
  48. static void flush_send_queue(struct cardstate *);
  49. /* transmit data from current open skb
  50. * result: number of bytes sent or error code < 0
  51. */
  52. static int write_modem(struct cardstate *cs)
  53. {
  54. struct tty_struct *tty = cs->hw.ser->tty;
  55. struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
  56. struct sk_buff *skb = bcs->tx_skb;
  57. int sent = -EOPNOTSUPP;
  58. WARN_ON(!tty || !tty->ops || !skb);
  59. if (!skb->len) {
  60. dev_kfree_skb_any(skb);
  61. bcs->tx_skb = NULL;
  62. return -EINVAL;
  63. }
  64. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  65. if (tty->ops->write)
  66. sent = tty->ops->write(tty, skb->data, skb->len);
  67. gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
  68. if (sent < 0) {
  69. /* error */
  70. flush_send_queue(cs);
  71. return sent;
  72. }
  73. skb_pull(skb, sent);
  74. if (!skb->len) {
  75. /* skb sent completely */
  76. gigaset_skb_sent(bcs, skb);
  77. gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
  78. (unsigned long) skb);
  79. dev_kfree_skb_any(skb);
  80. bcs->tx_skb = NULL;
  81. }
  82. return sent;
  83. }
  84. /*
  85. * transmit first queued command buffer
  86. * result: number of bytes sent or error code < 0
  87. */
  88. static int send_cb(struct cardstate *cs)
  89. {
  90. struct tty_struct *tty = cs->hw.ser->tty;
  91. struct cmdbuf_t *cb, *tcb;
  92. unsigned long flags;
  93. int sent = 0;
  94. WARN_ON(!tty || !tty->ops);
  95. cb = cs->cmdbuf;
  96. if (!cb)
  97. return 0; /* nothing to do */
  98. if (cb->len) {
  99. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  100. sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
  101. if (sent < 0) {
  102. /* error */
  103. gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
  104. flush_send_queue(cs);
  105. return sent;
  106. }
  107. cb->offset += sent;
  108. cb->len -= sent;
  109. gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
  110. sent, cb->len, cs->cmdbytes);
  111. }
  112. while (cb && !cb->len) {
  113. spin_lock_irqsave(&cs->cmdlock, flags);
  114. cs->cmdbytes -= cs->curlen;
  115. tcb = cb;
  116. cs->cmdbuf = cb = cb->next;
  117. if (cb) {
  118. cb->prev = NULL;
  119. cs->curlen = cb->len;
  120. } else {
  121. cs->lastcmdbuf = NULL;
  122. cs->curlen = 0;
  123. }
  124. spin_unlock_irqrestore(&cs->cmdlock, flags);
  125. if (tcb->wake_tasklet)
  126. tasklet_schedule(tcb->wake_tasklet);
  127. kfree(tcb);
  128. }
  129. return sent;
  130. }
  131. /*
  132. * send queue tasklet
  133. * If there is already a skb opened, put data to the transfer buffer
  134. * by calling "write_modem".
  135. * Otherwise take a new skb out of the queue.
  136. */
  137. static void gigaset_modem_fill(unsigned long data)
  138. {
  139. struct cardstate *cs = (struct cardstate *) data;
  140. struct bc_state *bcs;
  141. struct sk_buff *nextskb;
  142. int sent = 0;
  143. if (!cs) {
  144. gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
  145. return;
  146. }
  147. bcs = cs->bcs;
  148. if (!bcs) {
  149. gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
  150. return;
  151. }
  152. if (!bcs->tx_skb) {
  153. /* no skb is being sent; send command if any */
  154. sent = send_cb(cs);
  155. gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
  156. if (sent)
  157. /* something sent or error */
  158. return;
  159. /* no command to send; get skb */
  160. nextskb = skb_dequeue(&bcs->squeue);
  161. if (!nextskb)
  162. /* no skb either, nothing to do */
  163. return;
  164. bcs->tx_skb = nextskb;
  165. gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
  166. (unsigned long) bcs->tx_skb);
  167. }
  168. /* send skb */
  169. gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
  170. if (write_modem(cs) < 0)
  171. gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
  172. }
  173. /*
  174. * throw away all data queued for sending
  175. */
  176. static void flush_send_queue(struct cardstate *cs)
  177. {
  178. struct sk_buff *skb;
  179. struct cmdbuf_t *cb;
  180. unsigned long flags;
  181. /* command queue */
  182. spin_lock_irqsave(&cs->cmdlock, flags);
  183. while ((cb = cs->cmdbuf) != NULL) {
  184. cs->cmdbuf = cb->next;
  185. if (cb->wake_tasklet)
  186. tasklet_schedule(cb->wake_tasklet);
  187. kfree(cb);
  188. }
  189. cs->cmdbuf = cs->lastcmdbuf = NULL;
  190. cs->cmdbytes = cs->curlen = 0;
  191. spin_unlock_irqrestore(&cs->cmdlock, flags);
  192. /* data queue */
  193. if (cs->bcs->tx_skb)
  194. dev_kfree_skb_any(cs->bcs->tx_skb);
  195. while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
  196. dev_kfree_skb_any(skb);
  197. }
  198. /* Gigaset Driver Interface */
  199. /* ======================== */
  200. /*
  201. * queue an AT command string for transmission to the Gigaset device
  202. * parameters:
  203. * cs controller state structure
  204. * buf buffer containing the string to send
  205. * len number of characters to send
  206. * wake_tasklet tasklet to run when transmission is complete, or NULL
  207. * return value:
  208. * number of bytes queued, or error code < 0
  209. */
  210. static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
  211. {
  212. unsigned long flags;
  213. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  214. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  215. "CMD Transmit", cb->len, cb->buf);
  216. spin_lock_irqsave(&cs->cmdlock, flags);
  217. cb->prev = cs->lastcmdbuf;
  218. if (cs->lastcmdbuf)
  219. cs->lastcmdbuf->next = cb;
  220. else {
  221. cs->cmdbuf = cb;
  222. cs->curlen = cb->len;
  223. }
  224. cs->cmdbytes += cb->len;
  225. cs->lastcmdbuf = cb;
  226. spin_unlock_irqrestore(&cs->cmdlock, flags);
  227. spin_lock_irqsave(&cs->lock, flags);
  228. if (cs->connected)
  229. tasklet_schedule(&cs->write_tasklet);
  230. spin_unlock_irqrestore(&cs->lock, flags);
  231. return cb->len;
  232. }
  233. /*
  234. * tty_driver.write_room interface routine
  235. * return number of characters the driver will accept to be written
  236. * parameter:
  237. * controller state structure
  238. * return value:
  239. * number of characters
  240. */
  241. static int gigaset_write_room(struct cardstate *cs)
  242. {
  243. unsigned bytes;
  244. bytes = cs->cmdbytes;
  245. return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
  246. }
  247. /*
  248. * tty_driver.chars_in_buffer interface routine
  249. * return number of characters waiting to be sent
  250. * parameter:
  251. * controller state structure
  252. * return value:
  253. * number of characters
  254. */
  255. static int gigaset_chars_in_buffer(struct cardstate *cs)
  256. {
  257. return cs->cmdbytes;
  258. }
  259. /*
  260. * implementation of ioctl(GIGASET_BRKCHARS)
  261. * parameter:
  262. * controller state structure
  263. * return value:
  264. * -EINVAL (unimplemented function)
  265. */
  266. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  267. {
  268. /* not implemented */
  269. return -EINVAL;
  270. }
  271. /*
  272. * Open B channel
  273. * Called by "do_action" in ev-layer.c
  274. */
  275. static int gigaset_init_bchannel(struct bc_state *bcs)
  276. {
  277. /* nothing to do for M10x */
  278. gigaset_bchannel_up(bcs);
  279. return 0;
  280. }
  281. /*
  282. * Close B channel
  283. * Called by "do_action" in ev-layer.c
  284. */
  285. static int gigaset_close_bchannel(struct bc_state *bcs)
  286. {
  287. /* nothing to do for M10x */
  288. gigaset_bchannel_down(bcs);
  289. return 0;
  290. }
  291. /*
  292. * Set up B channel structure
  293. * This is called by "gigaset_initcs" in common.c
  294. */
  295. static int gigaset_initbcshw(struct bc_state *bcs)
  296. {
  297. /* unused */
  298. bcs->hw.ser = NULL;
  299. return 0;
  300. }
  301. /*
  302. * Free B channel structure
  303. * Called by "gigaset_freebcs" in common.c
  304. */
  305. static void gigaset_freebcshw(struct bc_state *bcs)
  306. {
  307. /* unused */
  308. }
  309. /*
  310. * Reinitialize B channel structure
  311. * This is called by "bcs_reinit" in common.c
  312. */
  313. static void gigaset_reinitbcshw(struct bc_state *bcs)
  314. {
  315. /* nothing to do for M10x */
  316. }
  317. /*
  318. * Free hardware specific device data
  319. * This will be called by "gigaset_freecs" in common.c
  320. */
  321. static void gigaset_freecshw(struct cardstate *cs)
  322. {
  323. tasklet_kill(&cs->write_tasklet);
  324. if (!cs->hw.ser)
  325. return;
  326. platform_device_unregister(&cs->hw.ser->dev);
  327. }
  328. static void gigaset_device_release(struct device *dev)
  329. {
  330. kfree(container_of(dev, struct ser_cardstate, dev.dev));
  331. }
  332. /*
  333. * Set up hardware specific device data
  334. * This is called by "gigaset_initcs" in common.c
  335. */
  336. static int gigaset_initcshw(struct cardstate *cs)
  337. {
  338. int rc;
  339. struct ser_cardstate *scs;
  340. scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
  341. if (!scs) {
  342. pr_err("out of memory\n");
  343. return -ENOMEM;
  344. }
  345. cs->hw.ser = scs;
  346. cs->hw.ser->dev.name = GIGASET_MODULENAME;
  347. cs->hw.ser->dev.id = cs->minor_index;
  348. cs->hw.ser->dev.dev.release = gigaset_device_release;
  349. rc = platform_device_register(&cs->hw.ser->dev);
  350. if (rc != 0) {
  351. pr_err("error %d registering platform device\n", rc);
  352. kfree(cs->hw.ser);
  353. cs->hw.ser = NULL;
  354. return rc;
  355. }
  356. tasklet_init(&cs->write_tasklet,
  357. gigaset_modem_fill, (unsigned long) cs);
  358. return 0;
  359. }
  360. /*
  361. * set modem control lines
  362. * Parameters:
  363. * card state structure
  364. * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
  365. * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
  366. * and by "if_lock" and "if_termios" in interface.c
  367. */
  368. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  369. unsigned new_state)
  370. {
  371. struct tty_struct *tty = cs->hw.ser->tty;
  372. unsigned int set, clear;
  373. WARN_ON(!tty || !tty->ops);
  374. /* tiocmset is an optional tty driver method */
  375. if (!tty->ops->tiocmset)
  376. return -EINVAL;
  377. set = new_state & ~old_state;
  378. clear = old_state & ~new_state;
  379. if (!set && !clear)
  380. return 0;
  381. gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
  382. return tty->ops->tiocmset(tty, set, clear);
  383. }
  384. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  385. {
  386. return -EINVAL;
  387. }
  388. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  389. {
  390. return -EINVAL;
  391. }
  392. static const struct gigaset_ops ops = {
  393. .write_cmd = gigaset_write_cmd,
  394. .write_room = gigaset_write_room,
  395. .chars_in_buffer = gigaset_chars_in_buffer,
  396. .brkchars = gigaset_brkchars,
  397. .init_bchannel = gigaset_init_bchannel,
  398. .close_bchannel = gigaset_close_bchannel,
  399. .initbcshw = gigaset_initbcshw,
  400. .freebcshw = gigaset_freebcshw,
  401. .reinitbcshw = gigaset_reinitbcshw,
  402. .initcshw = gigaset_initcshw,
  403. .freecshw = gigaset_freecshw,
  404. .set_modem_ctrl = gigaset_set_modem_ctrl,
  405. .baud_rate = gigaset_baud_rate,
  406. .set_line_ctrl = gigaset_set_line_ctrl,
  407. .send_skb = gigaset_m10x_send_skb, /* asyncdata.c */
  408. .handle_input = gigaset_m10x_input, /* asyncdata.c */
  409. };
  410. /* Line Discipline Interface */
  411. /* ========================= */
  412. /* helper functions for cardstate refcounting */
  413. static struct cardstate *cs_get(struct tty_struct *tty)
  414. {
  415. struct cardstate *cs = tty->disc_data;
  416. if (!cs || !cs->hw.ser) {
  417. gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
  418. return NULL;
  419. }
  420. atomic_inc(&cs->hw.ser->refcnt);
  421. return cs;
  422. }
  423. static void cs_put(struct cardstate *cs)
  424. {
  425. if (atomic_dec_and_test(&cs->hw.ser->refcnt))
  426. complete(&cs->hw.ser->dead_cmp);
  427. }
  428. /*
  429. * Called by the tty driver when the line discipline is pushed onto the tty.
  430. * Called in process context.
  431. */
  432. static int
  433. gigaset_tty_open(struct tty_struct *tty)
  434. {
  435. struct cardstate *cs;
  436. int rc;
  437. gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
  438. pr_info(DRIVER_DESC "\n");
  439. if (!driver) {
  440. pr_err("%s: no driver structure\n", __func__);
  441. return -ENODEV;
  442. }
  443. /* allocate memory for our device state and initialize it */
  444. cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
  445. if (!cs) {
  446. rc = -ENODEV;
  447. goto error;
  448. }
  449. cs->dev = &cs->hw.ser->dev.dev;
  450. cs->hw.ser->tty = tty;
  451. atomic_set(&cs->hw.ser->refcnt, 1);
  452. init_completion(&cs->hw.ser->dead_cmp);
  453. tty->disc_data = cs;
  454. /* Set the amount of data we're willing to receive per call
  455. * from the hardware driver to half of the input buffer size
  456. * to leave some reserve.
  457. * Note: We don't do flow control towards the hardware driver.
  458. * If more data is received than will fit into the input buffer,
  459. * it will be dropped and an error will be logged. This should
  460. * never happen as the device is slow and the buffer size ample.
  461. */
  462. tty->receive_room = RBUFSIZE/2;
  463. /* OK.. Initialization of the datastructures and the HW is done.. Now
  464. * startup system and notify the LL that we are ready to run
  465. */
  466. if (startmode == SM_LOCKED)
  467. cs->mstate = MS_LOCKED;
  468. rc = gigaset_start(cs);
  469. if (rc < 0) {
  470. tasklet_kill(&cs->write_tasklet);
  471. goto error;
  472. }
  473. gig_dbg(DEBUG_INIT, "Startup of HLL done");
  474. return 0;
  475. error:
  476. gig_dbg(DEBUG_INIT, "Startup of HLL failed");
  477. tty->disc_data = NULL;
  478. gigaset_freecs(cs);
  479. return rc;
  480. }
  481. /*
  482. * Called by the tty driver when the line discipline is removed.
  483. * Called from process context.
  484. */
  485. static void
  486. gigaset_tty_close(struct tty_struct *tty)
  487. {
  488. struct cardstate *cs = tty->disc_data;
  489. gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
  490. if (!cs) {
  491. gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
  492. return;
  493. }
  494. /* prevent other callers from entering ldisc methods */
  495. tty->disc_data = NULL;
  496. if (!cs->hw.ser)
  497. pr_err("%s: no hw cardstate\n", __func__);
  498. else {
  499. /* wait for running methods to finish */
  500. if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
  501. wait_for_completion(&cs->hw.ser->dead_cmp);
  502. }
  503. /* stop operations */
  504. gigaset_stop(cs);
  505. tasklet_kill(&cs->write_tasklet);
  506. flush_send_queue(cs);
  507. cs->dev = NULL;
  508. gigaset_freecs(cs);
  509. gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
  510. }
  511. /*
  512. * Called by the tty driver when the tty line is hung up.
  513. * Wait for I/O to driver to complete and unregister ISDN device.
  514. * This is already done by the close routine, so just call that.
  515. * Called from process context.
  516. */
  517. static int gigaset_tty_hangup(struct tty_struct *tty)
  518. {
  519. gigaset_tty_close(tty);
  520. return 0;
  521. }
  522. /*
  523. * Ioctl on the tty.
  524. * Called in process context only.
  525. * May be re-entered by multiple ioctl calling threads.
  526. */
  527. static int
  528. gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
  529. unsigned int cmd, unsigned long arg)
  530. {
  531. struct cardstate *cs = cs_get(tty);
  532. int rc, val;
  533. int __user *p = (int __user *)arg;
  534. if (!cs)
  535. return -ENXIO;
  536. switch (cmd) {
  537. case FIONREAD:
  538. /* unused, always return zero */
  539. val = 0;
  540. rc = put_user(val, p);
  541. break;
  542. case TCFLSH:
  543. /* flush our buffers and the serial port's buffer */
  544. switch (arg) {
  545. case TCIFLUSH:
  546. /* no own input buffer to flush */
  547. break;
  548. case TCIOFLUSH:
  549. case TCOFLUSH:
  550. flush_send_queue(cs);
  551. break;
  552. }
  553. /* Pass through */
  554. default:
  555. /* pass through to underlying serial device */
  556. rc = n_tty_ioctl_helper(tty, file, cmd, arg);
  557. break;
  558. }
  559. cs_put(cs);
  560. return rc;
  561. }
  562. /*
  563. * Called by the tty driver when a block of data has been received.
  564. * Will not be re-entered while running but other ldisc functions
  565. * may be called in parallel.
  566. * Can be called from hard interrupt level as well as soft interrupt
  567. * level or mainline.
  568. * Parameters:
  569. * tty tty structure
  570. * buf buffer containing received characters
  571. * cflags buffer containing error flags for received characters (ignored)
  572. * count number of received characters
  573. */
  574. static void
  575. gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
  576. char *cflags, int count)
  577. {
  578. struct cardstate *cs = cs_get(tty);
  579. unsigned tail, head, n;
  580. struct inbuf_t *inbuf;
  581. if (!cs)
  582. return;
  583. inbuf = cs->inbuf;
  584. if (!inbuf) {
  585. dev_err(cs->dev, "%s: no inbuf\n", __func__);
  586. cs_put(cs);
  587. return;
  588. }
  589. tail = inbuf->tail;
  590. head = inbuf->head;
  591. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
  592. head, tail, count);
  593. if (head <= tail) {
  594. /* possible buffer wraparound */
  595. n = min_t(unsigned, count, RBUFSIZE - tail);
  596. memcpy(inbuf->data + tail, buf, n);
  597. tail = (tail + n) % RBUFSIZE;
  598. buf += n;
  599. count -= n;
  600. }
  601. if (count > 0) {
  602. /* tail < head and some data left */
  603. n = head - tail - 1;
  604. if (count > n) {
  605. dev_err(cs->dev,
  606. "inbuf overflow, discarding %d bytes\n",
  607. count - n);
  608. count = n;
  609. }
  610. memcpy(inbuf->data + tail, buf, count);
  611. tail += count;
  612. }
  613. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  614. inbuf->tail = tail;
  615. /* Everything was received .. Push data into handler */
  616. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  617. gigaset_schedule_event(cs);
  618. cs_put(cs);
  619. }
  620. /*
  621. * Called by the tty driver when there's room for more data to send.
  622. */
  623. static void
  624. gigaset_tty_wakeup(struct tty_struct *tty)
  625. {
  626. struct cardstate *cs = cs_get(tty);
  627. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  628. if (!cs)
  629. return;
  630. tasklet_schedule(&cs->write_tasklet);
  631. cs_put(cs);
  632. }
  633. static struct tty_ldisc_ops gigaset_ldisc = {
  634. .owner = THIS_MODULE,
  635. .magic = TTY_LDISC_MAGIC,
  636. .name = "ser_gigaset",
  637. .open = gigaset_tty_open,
  638. .close = gigaset_tty_close,
  639. .hangup = gigaset_tty_hangup,
  640. .ioctl = gigaset_tty_ioctl,
  641. .receive_buf = gigaset_tty_receive,
  642. .write_wakeup = gigaset_tty_wakeup,
  643. };
  644. /* Initialization / Shutdown */
  645. /* ========================= */
  646. static int __init ser_gigaset_init(void)
  647. {
  648. int rc;
  649. gig_dbg(DEBUG_INIT, "%s", __func__);
  650. rc = platform_driver_register(&device_driver);
  651. if (rc != 0) {
  652. pr_err("error %d registering platform driver\n", rc);
  653. return rc;
  654. }
  655. /* allocate memory for our driver state and initialize it */
  656. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  657. GIGASET_MODULENAME, GIGASET_DEVNAME,
  658. &ops, THIS_MODULE);
  659. if (!driver) {
  660. rc = -ENOMEM;
  661. goto error;
  662. }
  663. rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
  664. if (rc != 0) {
  665. pr_err("error %d registering line discipline\n", rc);
  666. goto error;
  667. }
  668. return 0;
  669. error:
  670. if (driver) {
  671. gigaset_freedriver(driver);
  672. driver = NULL;
  673. }
  674. platform_driver_unregister(&device_driver);
  675. return rc;
  676. }
  677. static void __exit ser_gigaset_exit(void)
  678. {
  679. int rc;
  680. gig_dbg(DEBUG_INIT, "%s", __func__);
  681. if (driver) {
  682. gigaset_freedriver(driver);
  683. driver = NULL;
  684. }
  685. rc = tty_unregister_ldisc(N_GIGASET_M101);
  686. if (rc != 0)
  687. pr_err("error %d unregistering line discipline\n", rc);
  688. platform_driver_unregister(&device_driver);
  689. }
  690. module_init(ser_gigaset_init);
  691. module_exit(ser_gigaset_exit);