common.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Stuff used by all variants of the driver
  3. *
  4. * Copyright (c) 2001 by Stefan Eilers,
  5. * Hansjoerg Lipp <hjlipp@web.de>,
  6. * Tilman Schmidt <tilman@imap.cc>.
  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/module.h>
  17. #include <linux/moduleparam.h>
  18. /* Version Information */
  19. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  20. #define DRIVER_DESC "Driver for Gigaset 307x"
  21. #ifdef CONFIG_GIGASET_DEBUG
  22. #define DRIVER_DESC_DEBUG " (debug build)"
  23. #else
  24. #define DRIVER_DESC_DEBUG ""
  25. #endif
  26. /* Module parameters */
  27. int gigaset_debuglevel;
  28. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  29. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO | S_IWUSR);
  30. MODULE_PARM_DESC(debug, "debug level");
  31. /* driver state flags */
  32. #define VALID_MINOR 0x01
  33. #define VALID_ID 0x02
  34. /**
  35. * gigaset_dbg_buffer() - dump data in ASCII and hex for debugging
  36. * @level: debugging level.
  37. * @msg: message prefix.
  38. * @len: number of bytes to dump.
  39. * @buf: data to dump.
  40. *
  41. * If the current debugging level includes one of the bits set in @level,
  42. * @len bytes starting at @buf are logged to dmesg at KERN_DEBUG prio,
  43. * prefixed by the text @msg.
  44. */
  45. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  46. size_t len, const unsigned char *buf)
  47. {
  48. unsigned char outbuf[80];
  49. unsigned char c;
  50. size_t space = sizeof outbuf - 1;
  51. unsigned char *out = outbuf;
  52. size_t numin = len;
  53. while (numin--) {
  54. c = *buf++;
  55. if (c == '~' || c == '^' || c == '\\') {
  56. if (!space--)
  57. break;
  58. *out++ = '\\';
  59. }
  60. if (c & 0x80) {
  61. if (!space--)
  62. break;
  63. *out++ = '~';
  64. c ^= 0x80;
  65. }
  66. if (c < 0x20 || c == 0x7f) {
  67. if (!space--)
  68. break;
  69. *out++ = '^';
  70. c ^= 0x40;
  71. }
  72. if (!space--)
  73. break;
  74. *out++ = c;
  75. }
  76. *out = 0;
  77. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  78. }
  79. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  80. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  81. {
  82. int r;
  83. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  84. cs->control_state = flags;
  85. if (r < 0)
  86. return r;
  87. if (delay) {
  88. set_current_state(TASK_INTERRUPTIBLE);
  89. schedule_timeout(delay * HZ / 1000);
  90. }
  91. return 0;
  92. }
  93. int gigaset_enterconfigmode(struct cardstate *cs)
  94. {
  95. int i, r;
  96. cs->control_state = TIOCM_RTS;
  97. r = setflags(cs, TIOCM_DTR, 200);
  98. if (r < 0)
  99. goto error;
  100. r = setflags(cs, 0, 200);
  101. if (r < 0)
  102. goto error;
  103. for (i = 0; i < 5; ++i) {
  104. r = setflags(cs, TIOCM_RTS, 100);
  105. if (r < 0)
  106. goto error;
  107. r = setflags(cs, 0, 100);
  108. if (r < 0)
  109. goto error;
  110. }
  111. r = setflags(cs, TIOCM_RTS | TIOCM_DTR, 800);
  112. if (r < 0)
  113. goto error;
  114. return 0;
  115. error:
  116. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  117. cs->control_state = TIOCM_RTS | TIOCM_DTR;
  118. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS | TIOCM_DTR);
  119. return -1;
  120. }
  121. static int test_timeout(struct at_state_t *at_state)
  122. {
  123. if (!at_state->timer_expires)
  124. return 0;
  125. if (--at_state->timer_expires) {
  126. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  127. at_state, at_state->timer_expires);
  128. return 0;
  129. }
  130. gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  131. at_state->timer_index, NULL);
  132. return 1;
  133. }
  134. static void timer_tick(struct timer_list *t)
  135. {
  136. struct cardstate *cs = from_timer(cs, t, timer);
  137. unsigned long flags;
  138. unsigned channel;
  139. struct at_state_t *at_state;
  140. int timeout = 0;
  141. spin_lock_irqsave(&cs->lock, flags);
  142. for (channel = 0; channel < cs->channels; ++channel)
  143. if (test_timeout(&cs->bcs[channel].at_state))
  144. timeout = 1;
  145. if (test_timeout(&cs->at_state))
  146. timeout = 1;
  147. list_for_each_entry(at_state, &cs->temp_at_states, list)
  148. if (test_timeout(at_state))
  149. timeout = 1;
  150. if (cs->running) {
  151. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  152. if (timeout) {
  153. gig_dbg(DEBUG_EVENT, "scheduling timeout");
  154. tasklet_schedule(&cs->event_tasklet);
  155. }
  156. }
  157. spin_unlock_irqrestore(&cs->lock, flags);
  158. }
  159. int gigaset_get_channel(struct bc_state *bcs)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&bcs->cs->lock, flags);
  163. if (bcs->use_count || !try_module_get(bcs->cs->driver->owner)) {
  164. gig_dbg(DEBUG_CHANNEL, "could not allocate channel %d",
  165. bcs->channel);
  166. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  167. return -EBUSY;
  168. }
  169. ++bcs->use_count;
  170. bcs->busy = 1;
  171. gig_dbg(DEBUG_CHANNEL, "allocated channel %d", bcs->channel);
  172. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  173. return 0;
  174. }
  175. struct bc_state *gigaset_get_free_channel(struct cardstate *cs)
  176. {
  177. unsigned long flags;
  178. int i;
  179. spin_lock_irqsave(&cs->lock, flags);
  180. if (!try_module_get(cs->driver->owner)) {
  181. gig_dbg(DEBUG_CHANNEL,
  182. "could not get module for allocating channel");
  183. spin_unlock_irqrestore(&cs->lock, flags);
  184. return NULL;
  185. }
  186. for (i = 0; i < cs->channels; ++i)
  187. if (!cs->bcs[i].use_count) {
  188. ++cs->bcs[i].use_count;
  189. cs->bcs[i].busy = 1;
  190. spin_unlock_irqrestore(&cs->lock, flags);
  191. gig_dbg(DEBUG_CHANNEL, "allocated channel %d", i);
  192. return cs->bcs + i;
  193. }
  194. module_put(cs->driver->owner);
  195. spin_unlock_irqrestore(&cs->lock, flags);
  196. gig_dbg(DEBUG_CHANNEL, "no free channel");
  197. return NULL;
  198. }
  199. void gigaset_free_channel(struct bc_state *bcs)
  200. {
  201. unsigned long flags;
  202. spin_lock_irqsave(&bcs->cs->lock, flags);
  203. if (!bcs->busy) {
  204. gig_dbg(DEBUG_CHANNEL, "could not free channel %d",
  205. bcs->channel);
  206. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  207. return;
  208. }
  209. --bcs->use_count;
  210. bcs->busy = 0;
  211. module_put(bcs->cs->driver->owner);
  212. gig_dbg(DEBUG_CHANNEL, "freed channel %d", bcs->channel);
  213. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  214. }
  215. int gigaset_get_channels(struct cardstate *cs)
  216. {
  217. unsigned long flags;
  218. int i;
  219. spin_lock_irqsave(&cs->lock, flags);
  220. for (i = 0; i < cs->channels; ++i)
  221. if (cs->bcs[i].use_count) {
  222. spin_unlock_irqrestore(&cs->lock, flags);
  223. gig_dbg(DEBUG_CHANNEL,
  224. "could not allocate all channels");
  225. return -EBUSY;
  226. }
  227. for (i = 0; i < cs->channels; ++i)
  228. ++cs->bcs[i].use_count;
  229. spin_unlock_irqrestore(&cs->lock, flags);
  230. gig_dbg(DEBUG_CHANNEL, "allocated all channels");
  231. return 0;
  232. }
  233. void gigaset_free_channels(struct cardstate *cs)
  234. {
  235. unsigned long flags;
  236. int i;
  237. gig_dbg(DEBUG_CHANNEL, "unblocking all channels");
  238. spin_lock_irqsave(&cs->lock, flags);
  239. for (i = 0; i < cs->channels; ++i)
  240. --cs->bcs[i].use_count;
  241. spin_unlock_irqrestore(&cs->lock, flags);
  242. }
  243. void gigaset_block_channels(struct cardstate *cs)
  244. {
  245. unsigned long flags;
  246. int i;
  247. gig_dbg(DEBUG_CHANNEL, "blocking all channels");
  248. spin_lock_irqsave(&cs->lock, flags);
  249. for (i = 0; i < cs->channels; ++i)
  250. ++cs->bcs[i].use_count;
  251. spin_unlock_irqrestore(&cs->lock, flags);
  252. }
  253. static void clear_events(struct cardstate *cs)
  254. {
  255. struct event_t *ev;
  256. unsigned head, tail;
  257. unsigned long flags;
  258. spin_lock_irqsave(&cs->ev_lock, flags);
  259. head = cs->ev_head;
  260. tail = cs->ev_tail;
  261. while (tail != head) {
  262. ev = cs->events + head;
  263. kfree(ev->ptr);
  264. head = (head + 1) % MAX_EVENTS;
  265. }
  266. cs->ev_head = tail;
  267. spin_unlock_irqrestore(&cs->ev_lock, flags);
  268. }
  269. /**
  270. * gigaset_add_event() - add event to device event queue
  271. * @cs: device descriptor structure.
  272. * @at_state: connection state structure.
  273. * @type: event type.
  274. * @ptr: pointer parameter for event.
  275. * @parameter: integer parameter for event.
  276. * @arg: pointer parameter for event.
  277. *
  278. * Allocate an event queue entry from the device's event queue, and set it up
  279. * with the parameters given.
  280. *
  281. * Return value: added event
  282. */
  283. struct event_t *gigaset_add_event(struct cardstate *cs,
  284. struct at_state_t *at_state, int type,
  285. void *ptr, int parameter, void *arg)
  286. {
  287. unsigned long flags;
  288. unsigned next, tail;
  289. struct event_t *event = NULL;
  290. gig_dbg(DEBUG_EVENT, "queueing event %d", type);
  291. spin_lock_irqsave(&cs->ev_lock, flags);
  292. tail = cs->ev_tail;
  293. next = (tail + 1) % MAX_EVENTS;
  294. if (unlikely(next == cs->ev_head))
  295. dev_err(cs->dev, "event queue full\n");
  296. else {
  297. event = cs->events + tail;
  298. event->type = type;
  299. event->at_state = at_state;
  300. event->cid = -1;
  301. event->ptr = ptr;
  302. event->arg = arg;
  303. event->parameter = parameter;
  304. cs->ev_tail = next;
  305. }
  306. spin_unlock_irqrestore(&cs->ev_lock, flags);
  307. return event;
  308. }
  309. EXPORT_SYMBOL_GPL(gigaset_add_event);
  310. static void clear_at_state(struct at_state_t *at_state)
  311. {
  312. int i;
  313. for (i = 0; i < STR_NUM; ++i) {
  314. kfree(at_state->str_var[i]);
  315. at_state->str_var[i] = NULL;
  316. }
  317. }
  318. static void dealloc_temp_at_states(struct cardstate *cs)
  319. {
  320. struct at_state_t *cur, *next;
  321. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  322. list_del(&cur->list);
  323. clear_at_state(cur);
  324. kfree(cur);
  325. }
  326. }
  327. static void gigaset_freebcs(struct bc_state *bcs)
  328. {
  329. int i;
  330. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  331. bcs->cs->ops->freebcshw(bcs);
  332. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  333. clear_at_state(&bcs->at_state);
  334. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  335. dev_kfree_skb(bcs->rx_skb);
  336. bcs->rx_skb = NULL;
  337. for (i = 0; i < AT_NUM; ++i) {
  338. kfree(bcs->commands[i]);
  339. bcs->commands[i] = NULL;
  340. }
  341. }
  342. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  343. {
  344. unsigned long flags;
  345. unsigned i;
  346. struct cardstate *cs;
  347. struct cardstate *ret = NULL;
  348. spin_lock_irqsave(&drv->lock, flags);
  349. if (drv->blocked)
  350. goto exit;
  351. for (i = 0; i < drv->minors; ++i) {
  352. cs = drv->cs + i;
  353. if (!(cs->flags & VALID_MINOR)) {
  354. cs->flags = VALID_MINOR;
  355. ret = cs;
  356. break;
  357. }
  358. }
  359. exit:
  360. spin_unlock_irqrestore(&drv->lock, flags);
  361. return ret;
  362. }
  363. static void free_cs(struct cardstate *cs)
  364. {
  365. cs->flags = 0;
  366. }
  367. static void make_valid(struct cardstate *cs, unsigned mask)
  368. {
  369. unsigned long flags;
  370. struct gigaset_driver *drv = cs->driver;
  371. spin_lock_irqsave(&drv->lock, flags);
  372. cs->flags |= mask;
  373. spin_unlock_irqrestore(&drv->lock, flags);
  374. }
  375. static void make_invalid(struct cardstate *cs, unsigned mask)
  376. {
  377. unsigned long flags;
  378. struct gigaset_driver *drv = cs->driver;
  379. spin_lock_irqsave(&drv->lock, flags);
  380. cs->flags &= ~mask;
  381. spin_unlock_irqrestore(&drv->lock, flags);
  382. }
  383. /**
  384. * gigaset_freecs() - free all associated ressources of a device
  385. * @cs: device descriptor structure.
  386. *
  387. * Stops all tasklets and timers, unregisters the device from all
  388. * subsystems it was registered to, deallocates the device structure
  389. * @cs and all structures referenced from it.
  390. * Operations on the device should be stopped before calling this.
  391. */
  392. void gigaset_freecs(struct cardstate *cs)
  393. {
  394. int i;
  395. unsigned long flags;
  396. if (!cs)
  397. return;
  398. mutex_lock(&cs->mutex);
  399. spin_lock_irqsave(&cs->lock, flags);
  400. cs->running = 0;
  401. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  402. not rescheduled below */
  403. tasklet_kill(&cs->event_tasklet);
  404. del_timer_sync(&cs->timer);
  405. switch (cs->cs_init) {
  406. default:
  407. /* clear B channel structures */
  408. for (i = 0; i < cs->channels; ++i) {
  409. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  410. gigaset_freebcs(cs->bcs + i);
  411. }
  412. /* clear device sysfs */
  413. gigaset_free_dev_sysfs(cs);
  414. gigaset_if_free(cs);
  415. gig_dbg(DEBUG_INIT, "clearing hw");
  416. cs->ops->freecshw(cs);
  417. /* fall through */
  418. case 2: /* error in initcshw */
  419. /* Deregister from LL */
  420. make_invalid(cs, VALID_ID);
  421. gigaset_isdn_unregdev(cs);
  422. /* fall through */
  423. case 1: /* error when registering to LL */
  424. gig_dbg(DEBUG_INIT, "clearing at_state");
  425. clear_at_state(&cs->at_state);
  426. dealloc_temp_at_states(cs);
  427. clear_events(cs);
  428. tty_port_destroy(&cs->port);
  429. /* fall through */
  430. case 0: /* error in basic setup */
  431. gig_dbg(DEBUG_INIT, "freeing inbuf");
  432. kfree(cs->inbuf);
  433. kfree(cs->bcs);
  434. }
  435. mutex_unlock(&cs->mutex);
  436. free_cs(cs);
  437. }
  438. EXPORT_SYMBOL_GPL(gigaset_freecs);
  439. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  440. struct cardstate *cs, int cid)
  441. {
  442. int i;
  443. INIT_LIST_HEAD(&at_state->list);
  444. at_state->waiting = 0;
  445. at_state->getstring = 0;
  446. at_state->pending_commands = 0;
  447. at_state->timer_expires = 0;
  448. at_state->timer_active = 0;
  449. at_state->timer_index = 0;
  450. at_state->seq_index = 0;
  451. at_state->ConState = 0;
  452. for (i = 0; i < STR_NUM; ++i)
  453. at_state->str_var[i] = NULL;
  454. at_state->int_var[VAR_ZDLE] = 0;
  455. at_state->int_var[VAR_ZCTP] = -1;
  456. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  457. at_state->cs = cs;
  458. at_state->bcs = bcs;
  459. at_state->cid = cid;
  460. if (!cid)
  461. at_state->replystruct = cs->tabnocid;
  462. else
  463. at_state->replystruct = cs->tabcid;
  464. }
  465. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct cardstate *cs)
  466. /* inbuf->read must be allocated before! */
  467. {
  468. inbuf->head = 0;
  469. inbuf->tail = 0;
  470. inbuf->cs = cs;
  471. inbuf->inputstate = INS_command;
  472. }
  473. /**
  474. * gigaset_fill_inbuf() - append received data to input buffer
  475. * @inbuf: buffer structure.
  476. * @src: received data.
  477. * @numbytes: number of bytes received.
  478. *
  479. * Return value: !=0 if some data was appended
  480. */
  481. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  482. unsigned numbytes)
  483. {
  484. unsigned n, head, tail, bytesleft;
  485. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  486. if (!numbytes)
  487. return 0;
  488. bytesleft = numbytes;
  489. tail = inbuf->tail;
  490. head = inbuf->head;
  491. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  492. while (bytesleft) {
  493. if (head > tail)
  494. n = head - 1 - tail;
  495. else if (head == 0)
  496. n = (RBUFSIZE - 1) - tail;
  497. else
  498. n = RBUFSIZE - tail;
  499. if (!n) {
  500. dev_err(inbuf->cs->dev,
  501. "buffer overflow (%u bytes lost)\n",
  502. bytesleft);
  503. break;
  504. }
  505. if (n > bytesleft)
  506. n = bytesleft;
  507. memcpy(inbuf->data + tail, src, n);
  508. bytesleft -= n;
  509. tail = (tail + n) % RBUFSIZE;
  510. src += n;
  511. }
  512. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  513. inbuf->tail = tail;
  514. return numbytes != bytesleft;
  515. }
  516. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  517. /* Initialize the b-channel structure */
  518. static int gigaset_initbcs(struct bc_state *bcs, struct cardstate *cs,
  519. int channel)
  520. {
  521. int i;
  522. bcs->tx_skb = NULL;
  523. skb_queue_head_init(&bcs->squeue);
  524. bcs->corrupted = 0;
  525. bcs->trans_down = 0;
  526. bcs->trans_up = 0;
  527. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  528. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  529. #ifdef CONFIG_GIGASET_DEBUG
  530. bcs->emptycount = 0;
  531. #endif
  532. bcs->rx_bufsize = 0;
  533. bcs->rx_skb = NULL;
  534. bcs->rx_fcs = PPP_INITFCS;
  535. bcs->inputstate = 0;
  536. bcs->channel = channel;
  537. bcs->cs = cs;
  538. bcs->chstate = 0;
  539. bcs->use_count = 1;
  540. bcs->busy = 0;
  541. bcs->ignore = cs->ignoreframes;
  542. for (i = 0; i < AT_NUM; ++i)
  543. bcs->commands[i] = NULL;
  544. spin_lock_init(&bcs->aplock);
  545. bcs->ap = NULL;
  546. bcs->apconnstate = 0;
  547. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  548. return cs->ops->initbcshw(bcs);
  549. }
  550. /**
  551. * gigaset_initcs() - initialize device structure
  552. * @drv: hardware driver the device belongs to
  553. * @channels: number of B channels supported by device
  554. * @onechannel: !=0 if B channel data and AT commands share one
  555. * communication channel (M10x),
  556. * ==0 if B channels have separate communication channels (base)
  557. * @ignoreframes: number of frames to ignore after setting up B channel
  558. * @cidmode: !=0: start in CallID mode
  559. * @modulename: name of driver module for LL registration
  560. *
  561. * Allocate and initialize cardstate structure for Gigaset driver
  562. * Calls hardware dependent gigaset_initcshw() function
  563. * Calls B channel initialization function gigaset_initbcs() for each B channel
  564. *
  565. * Return value:
  566. * pointer to cardstate structure
  567. */
  568. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  569. int onechannel, int ignoreframes,
  570. int cidmode, const char *modulename)
  571. {
  572. struct cardstate *cs;
  573. unsigned long flags;
  574. int i;
  575. gig_dbg(DEBUG_INIT, "allocating cs");
  576. cs = alloc_cs(drv);
  577. if (!cs) {
  578. pr_err("maximum number of devices exceeded\n");
  579. return NULL;
  580. }
  581. cs->cs_init = 0;
  582. cs->channels = channels;
  583. cs->onechannel = onechannel;
  584. cs->ignoreframes = ignoreframes;
  585. INIT_LIST_HEAD(&cs->temp_at_states);
  586. cs->running = 0;
  587. timer_setup(&cs->timer, timer_tick, 0);
  588. spin_lock_init(&cs->ev_lock);
  589. cs->ev_tail = 0;
  590. cs->ev_head = 0;
  591. tasklet_init(&cs->event_tasklet, gigaset_handle_event,
  592. (unsigned long) cs);
  593. tty_port_init(&cs->port);
  594. cs->commands_pending = 0;
  595. cs->cur_at_seq = 0;
  596. cs->gotfwver = -1;
  597. cs->dev = NULL;
  598. cs->tty_dev = NULL;
  599. cs->cidmode = cidmode != 0;
  600. cs->tabnocid = gigaset_tab_nocid;
  601. cs->tabcid = gigaset_tab_cid;
  602. init_waitqueue_head(&cs->waitqueue);
  603. cs->waiting = 0;
  604. cs->mode = M_UNKNOWN;
  605. cs->mstate = MS_UNINITIALIZED;
  606. cs->bcs = kmalloc_array(channels, sizeof(struct bc_state), GFP_KERNEL);
  607. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  608. if (!cs->bcs || !cs->inbuf) {
  609. pr_err("out of memory\n");
  610. goto error;
  611. }
  612. ++cs->cs_init;
  613. gig_dbg(DEBUG_INIT, "setting up at_state");
  614. spin_lock_init(&cs->lock);
  615. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  616. cs->dle = 0;
  617. cs->cbytes = 0;
  618. gig_dbg(DEBUG_INIT, "setting up inbuf");
  619. gigaset_inbuf_init(cs->inbuf, cs);
  620. cs->connected = 0;
  621. cs->isdn_up = 0;
  622. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  623. cs->cmdbuf = cs->lastcmdbuf = NULL;
  624. spin_lock_init(&cs->cmdlock);
  625. cs->curlen = 0;
  626. cs->cmdbytes = 0;
  627. gig_dbg(DEBUG_INIT, "setting up iif");
  628. if (gigaset_isdn_regdev(cs, modulename) < 0) {
  629. pr_err("error registering ISDN device\n");
  630. goto error;
  631. }
  632. make_valid(cs, VALID_ID);
  633. ++cs->cs_init;
  634. gig_dbg(DEBUG_INIT, "setting up hw");
  635. if (cs->ops->initcshw(cs) < 0)
  636. goto error;
  637. ++cs->cs_init;
  638. /* set up character device */
  639. gigaset_if_init(cs);
  640. /* set up device sysfs */
  641. gigaset_init_dev_sysfs(cs);
  642. /* set up channel data structures */
  643. for (i = 0; i < channels; ++i) {
  644. gig_dbg(DEBUG_INIT, "setting up bcs[%d]", i);
  645. if (gigaset_initbcs(cs->bcs + i, cs, i) < 0) {
  646. pr_err("could not allocate channel %d data\n", i);
  647. goto error;
  648. }
  649. }
  650. spin_lock_irqsave(&cs->lock, flags);
  651. cs->running = 1;
  652. spin_unlock_irqrestore(&cs->lock, flags);
  653. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  654. add_timer(&cs->timer);
  655. gig_dbg(DEBUG_INIT, "cs initialized");
  656. return cs;
  657. error:
  658. gig_dbg(DEBUG_INIT, "failed");
  659. gigaset_freecs(cs);
  660. return NULL;
  661. }
  662. EXPORT_SYMBOL_GPL(gigaset_initcs);
  663. /* ReInitialize the b-channel structure on hangup */
  664. void gigaset_bcs_reinit(struct bc_state *bcs)
  665. {
  666. struct sk_buff *skb;
  667. struct cardstate *cs = bcs->cs;
  668. unsigned long flags;
  669. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  670. dev_kfree_skb(skb);
  671. spin_lock_irqsave(&cs->lock, flags);
  672. clear_at_state(&bcs->at_state);
  673. bcs->at_state.ConState = 0;
  674. bcs->at_state.timer_active = 0;
  675. bcs->at_state.timer_expires = 0;
  676. bcs->at_state.cid = -1; /* No CID defined */
  677. spin_unlock_irqrestore(&cs->lock, flags);
  678. bcs->inputstate = 0;
  679. #ifdef CONFIG_GIGASET_DEBUG
  680. bcs->emptycount = 0;
  681. #endif
  682. bcs->rx_fcs = PPP_INITFCS;
  683. bcs->chstate = 0;
  684. bcs->ignore = cs->ignoreframes;
  685. dev_kfree_skb(bcs->rx_skb);
  686. bcs->rx_skb = NULL;
  687. cs->ops->reinitbcshw(bcs);
  688. }
  689. static void cleanup_cs(struct cardstate *cs)
  690. {
  691. struct cmdbuf_t *cb, *tcb;
  692. int i;
  693. unsigned long flags;
  694. spin_lock_irqsave(&cs->lock, flags);
  695. cs->mode = M_UNKNOWN;
  696. cs->mstate = MS_UNINITIALIZED;
  697. clear_at_state(&cs->at_state);
  698. dealloc_temp_at_states(cs);
  699. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  700. cs->inbuf->inputstate = INS_command;
  701. cs->inbuf->head = 0;
  702. cs->inbuf->tail = 0;
  703. cb = cs->cmdbuf;
  704. while (cb) {
  705. tcb = cb;
  706. cb = cb->next;
  707. kfree(tcb);
  708. }
  709. cs->cmdbuf = cs->lastcmdbuf = NULL;
  710. cs->curlen = 0;
  711. cs->cmdbytes = 0;
  712. cs->gotfwver = -1;
  713. cs->dle = 0;
  714. cs->cur_at_seq = 0;
  715. cs->commands_pending = 0;
  716. cs->cbytes = 0;
  717. spin_unlock_irqrestore(&cs->lock, flags);
  718. for (i = 0; i < cs->channels; ++i) {
  719. gigaset_freebcs(cs->bcs + i);
  720. if (gigaset_initbcs(cs->bcs + i, cs, i) < 0)
  721. pr_err("could not allocate channel %d data\n", i);
  722. }
  723. if (cs->waiting) {
  724. cs->cmd_result = -ENODEV;
  725. cs->waiting = 0;
  726. wake_up_interruptible(&cs->waitqueue);
  727. }
  728. }
  729. /**
  730. * gigaset_start() - start device operations
  731. * @cs: device descriptor structure.
  732. *
  733. * Prepares the device for use by setting up communication parameters,
  734. * scheduling an EV_START event to initiate device initialization, and
  735. * waiting for completion of the initialization.
  736. *
  737. * Return value:
  738. * 0 on success, error code < 0 on failure
  739. */
  740. int gigaset_start(struct cardstate *cs)
  741. {
  742. unsigned long flags;
  743. if (mutex_lock_interruptible(&cs->mutex))
  744. return -EBUSY;
  745. spin_lock_irqsave(&cs->lock, flags);
  746. cs->connected = 1;
  747. spin_unlock_irqrestore(&cs->lock, flags);
  748. if (cs->mstate != MS_LOCKED) {
  749. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR | TIOCM_RTS);
  750. cs->ops->baud_rate(cs, B115200);
  751. cs->ops->set_line_ctrl(cs, CS8);
  752. cs->control_state = TIOCM_DTR | TIOCM_RTS;
  753. }
  754. cs->waiting = 1;
  755. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  756. cs->waiting = 0;
  757. goto error;
  758. }
  759. gigaset_schedule_event(cs);
  760. wait_event(cs->waitqueue, !cs->waiting);
  761. mutex_unlock(&cs->mutex);
  762. return 0;
  763. error:
  764. mutex_unlock(&cs->mutex);
  765. return -ENOMEM;
  766. }
  767. EXPORT_SYMBOL_GPL(gigaset_start);
  768. /**
  769. * gigaset_shutdown() - shut down device operations
  770. * @cs: device descriptor structure.
  771. *
  772. * Deactivates the device by scheduling an EV_SHUTDOWN event and
  773. * waiting for completion of the shutdown.
  774. *
  775. * Return value:
  776. * 0 - success, -ENODEV - error (no device associated)
  777. */
  778. int gigaset_shutdown(struct cardstate *cs)
  779. {
  780. mutex_lock(&cs->mutex);
  781. if (!(cs->flags & VALID_MINOR)) {
  782. mutex_unlock(&cs->mutex);
  783. return -ENODEV;
  784. }
  785. cs->waiting = 1;
  786. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL))
  787. goto exit;
  788. gigaset_schedule_event(cs);
  789. wait_event(cs->waitqueue, !cs->waiting);
  790. cleanup_cs(cs);
  791. exit:
  792. mutex_unlock(&cs->mutex);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  796. /**
  797. * gigaset_stop() - stop device operations
  798. * @cs: device descriptor structure.
  799. *
  800. * Stops operations on the device by scheduling an EV_STOP event and
  801. * waiting for completion of the shutdown.
  802. */
  803. void gigaset_stop(struct cardstate *cs)
  804. {
  805. mutex_lock(&cs->mutex);
  806. cs->waiting = 1;
  807. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL))
  808. goto exit;
  809. gigaset_schedule_event(cs);
  810. wait_event(cs->waitqueue, !cs->waiting);
  811. cleanup_cs(cs);
  812. exit:
  813. mutex_unlock(&cs->mutex);
  814. }
  815. EXPORT_SYMBOL_GPL(gigaset_stop);
  816. static LIST_HEAD(drivers);
  817. static DEFINE_SPINLOCK(driver_lock);
  818. struct cardstate *gigaset_get_cs_by_id(int id)
  819. {
  820. unsigned long flags;
  821. struct cardstate *ret = NULL;
  822. struct cardstate *cs;
  823. struct gigaset_driver *drv;
  824. unsigned i;
  825. spin_lock_irqsave(&driver_lock, flags);
  826. list_for_each_entry(drv, &drivers, list) {
  827. spin_lock(&drv->lock);
  828. for (i = 0; i < drv->minors; ++i) {
  829. cs = drv->cs + i;
  830. if ((cs->flags & VALID_ID) && cs->myid == id) {
  831. ret = cs;
  832. break;
  833. }
  834. }
  835. spin_unlock(&drv->lock);
  836. if (ret)
  837. break;
  838. }
  839. spin_unlock_irqrestore(&driver_lock, flags);
  840. return ret;
  841. }
  842. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  843. {
  844. unsigned long flags;
  845. struct cardstate *ret = NULL;
  846. struct gigaset_driver *drv;
  847. unsigned index;
  848. spin_lock_irqsave(&driver_lock, flags);
  849. list_for_each_entry(drv, &drivers, list) {
  850. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  851. continue;
  852. index = minor - drv->minor;
  853. spin_lock(&drv->lock);
  854. if (drv->cs[index].flags & VALID_MINOR)
  855. ret = drv->cs + index;
  856. spin_unlock(&drv->lock);
  857. if (ret)
  858. break;
  859. }
  860. spin_unlock_irqrestore(&driver_lock, flags);
  861. return ret;
  862. }
  863. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  864. {
  865. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  866. }
  867. /**
  868. * gigaset_freedriver() - free all associated ressources of a driver
  869. * @drv: driver descriptor structure.
  870. *
  871. * Unregisters the driver from the system and deallocates the driver
  872. * structure @drv and all structures referenced from it.
  873. * All devices should be shut down before calling this.
  874. */
  875. void gigaset_freedriver(struct gigaset_driver *drv)
  876. {
  877. unsigned long flags;
  878. spin_lock_irqsave(&driver_lock, flags);
  879. list_del(&drv->list);
  880. spin_unlock_irqrestore(&driver_lock, flags);
  881. gigaset_if_freedriver(drv);
  882. kfree(drv->cs);
  883. kfree(drv);
  884. }
  885. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  886. /**
  887. * gigaset_initdriver() - initialize driver structure
  888. * @minor: First minor number
  889. * @minors: Number of minors this driver can handle
  890. * @procname: Name of the driver
  891. * @devname: Name of the device files (prefix without minor number)
  892. *
  893. * Allocate and initialize gigaset_driver structure. Initialize interface.
  894. *
  895. * Return value:
  896. * Pointer to the gigaset_driver structure on success, NULL on failure.
  897. */
  898. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  899. const char *procname,
  900. const char *devname,
  901. const struct gigaset_ops *ops,
  902. struct module *owner)
  903. {
  904. struct gigaset_driver *drv;
  905. unsigned long flags;
  906. unsigned i;
  907. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  908. if (!drv)
  909. return NULL;
  910. drv->have_tty = 0;
  911. drv->minor = minor;
  912. drv->minors = minors;
  913. spin_lock_init(&drv->lock);
  914. drv->blocked = 0;
  915. drv->ops = ops;
  916. drv->owner = owner;
  917. INIT_LIST_HEAD(&drv->list);
  918. drv->cs = kmalloc_array(minors, sizeof(*drv->cs), GFP_KERNEL);
  919. if (!drv->cs)
  920. goto error;
  921. for (i = 0; i < minors; ++i) {
  922. drv->cs[i].flags = 0;
  923. drv->cs[i].driver = drv;
  924. drv->cs[i].ops = drv->ops;
  925. drv->cs[i].minor_index = i;
  926. mutex_init(&drv->cs[i].mutex);
  927. }
  928. gigaset_if_initdriver(drv, procname, devname);
  929. spin_lock_irqsave(&driver_lock, flags);
  930. list_add(&drv->list, &drivers);
  931. spin_unlock_irqrestore(&driver_lock, flags);
  932. return drv;
  933. error:
  934. kfree(drv);
  935. return NULL;
  936. }
  937. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  938. /**
  939. * gigaset_blockdriver() - block driver
  940. * @drv: driver descriptor structure.
  941. *
  942. * Prevents the driver from attaching new devices, in preparation for
  943. * deregistration.
  944. */
  945. void gigaset_blockdriver(struct gigaset_driver *drv)
  946. {
  947. drv->blocked = 1;
  948. }
  949. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  950. static int __init gigaset_init_module(void)
  951. {
  952. /* in accordance with the principle of least astonishment,
  953. * setting the 'debug' parameter to 1 activates a sensible
  954. * set of default debug levels
  955. */
  956. if (gigaset_debuglevel == 1)
  957. gigaset_debuglevel = DEBUG_DEFAULT;
  958. pr_info(DRIVER_DESC DRIVER_DESC_DEBUG "\n");
  959. gigaset_isdn_regdrv();
  960. return 0;
  961. }
  962. static void __exit gigaset_exit_module(void)
  963. {
  964. gigaset_isdn_unregdrv();
  965. }
  966. module_init(gigaset_init_module);
  967. module_exit(gigaset_exit_module);
  968. MODULE_AUTHOR(DRIVER_AUTHOR);
  969. MODULE_DESCRIPTION(DRIVER_DESC);
  970. MODULE_LICENSE("GPL");