ser-gigaset.c 19 KB

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