ser-gigaset.c 19 KB

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