common.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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/ctype.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Tilman Schmidt <tilman@imap.cc>, Stefan Eilers"
  21. #define DRIVER_DESC "Driver for Gigaset 307x"
  22. /* Module parameters */
  23. int gigaset_debuglevel = DEBUG_DEFAULT;
  24. EXPORT_SYMBOL_GPL(gigaset_debuglevel);
  25. module_param_named(debug, gigaset_debuglevel, int, S_IRUGO|S_IWUSR);
  26. MODULE_PARM_DESC(debug, "debug level");
  27. /* driver state flags */
  28. #define VALID_MINOR 0x01
  29. #define VALID_ID 0x02
  30. #define ASSIGNED 0x04
  31. /* bitwise byte inversion table */
  32. __u8 gigaset_invtab[256] = {
  33. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  34. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  35. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  36. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  37. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  38. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  39. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  40. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  41. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  42. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  43. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  44. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  45. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  46. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  47. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  48. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  49. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  50. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  51. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  52. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  53. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  54. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  55. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  56. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  57. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  58. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  59. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  60. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  61. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  62. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  63. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  64. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  65. };
  66. EXPORT_SYMBOL_GPL(gigaset_invtab);
  67. void gigaset_dbg_buffer(enum debuglevel level, const unsigned char *msg,
  68. size_t len, const unsigned char *buf)
  69. {
  70. unsigned char outbuf[80];
  71. unsigned char c;
  72. size_t space = sizeof outbuf - 1;
  73. unsigned char *out = outbuf;
  74. size_t numin = len;
  75. while (numin--) {
  76. c = *buf++;
  77. if (c == '~' || c == '^' || c == '\\') {
  78. if (!space--)
  79. break;
  80. *out++ = '\\';
  81. }
  82. if (c & 0x80) {
  83. if (!space--)
  84. break;
  85. *out++ = '~';
  86. c ^= 0x80;
  87. }
  88. if (c < 0x20 || c == 0x7f) {
  89. if (!space--)
  90. break;
  91. *out++ = '^';
  92. c ^= 0x40;
  93. }
  94. if (!space--)
  95. break;
  96. *out++ = c;
  97. }
  98. *out = 0;
  99. gig_dbg(level, "%s (%u bytes): %s", msg, (unsigned) len, outbuf);
  100. }
  101. EXPORT_SYMBOL_GPL(gigaset_dbg_buffer);
  102. static int setflags(struct cardstate *cs, unsigned flags, unsigned delay)
  103. {
  104. int r;
  105. r = cs->ops->set_modem_ctrl(cs, cs->control_state, flags);
  106. cs->control_state = flags;
  107. if (r < 0)
  108. return r;
  109. if (delay) {
  110. set_current_state(TASK_INTERRUPTIBLE);
  111. schedule_timeout(delay * HZ / 1000);
  112. }
  113. return 0;
  114. }
  115. int gigaset_enterconfigmode(struct cardstate *cs)
  116. {
  117. int i, r;
  118. cs->control_state = TIOCM_RTS; //FIXME
  119. r = setflags(cs, TIOCM_DTR, 200);
  120. if (r < 0)
  121. goto error;
  122. r = setflags(cs, 0, 200);
  123. if (r < 0)
  124. goto error;
  125. for (i = 0; i < 5; ++i) {
  126. r = setflags(cs, TIOCM_RTS, 100);
  127. if (r < 0)
  128. goto error;
  129. r = setflags(cs, 0, 100);
  130. if (r < 0)
  131. goto error;
  132. }
  133. r = setflags(cs, TIOCM_RTS|TIOCM_DTR, 800);
  134. if (r < 0)
  135. goto error;
  136. return 0;
  137. error:
  138. dev_err(cs->dev, "error %d on setuartbits\n", -r);
  139. cs->control_state = TIOCM_RTS|TIOCM_DTR; // FIXME is this a good value?
  140. cs->ops->set_modem_ctrl(cs, 0, TIOCM_RTS|TIOCM_DTR);
  141. return -1; //r
  142. }
  143. static int test_timeout(struct at_state_t *at_state)
  144. {
  145. if (!at_state->timer_expires)
  146. return 0;
  147. if (--at_state->timer_expires) {
  148. gig_dbg(DEBUG_MCMD, "decreased timer of %p to %lu",
  149. at_state, at_state->timer_expires);
  150. return 0;
  151. }
  152. if (!gigaset_add_event(at_state->cs, at_state, EV_TIMEOUT, NULL,
  153. at_state->timer_index, NULL)) {
  154. //FIXME what should we do?
  155. }
  156. return 1;
  157. }
  158. static void timer_tick(unsigned long data)
  159. {
  160. struct cardstate *cs = (struct cardstate *) data;
  161. unsigned long flags;
  162. unsigned channel;
  163. struct at_state_t *at_state;
  164. int timeout = 0;
  165. spin_lock_irqsave(&cs->lock, flags);
  166. for (channel = 0; channel < cs->channels; ++channel)
  167. if (test_timeout(&cs->bcs[channel].at_state))
  168. timeout = 1;
  169. if (test_timeout(&cs->at_state))
  170. timeout = 1;
  171. list_for_each_entry(at_state, &cs->temp_at_states, list)
  172. if (test_timeout(at_state))
  173. timeout = 1;
  174. if (cs->running) {
  175. mod_timer(&cs->timer, jiffies + msecs_to_jiffies(GIG_TICK));
  176. if (timeout) {
  177. gig_dbg(DEBUG_CMD, "scheduling timeout");
  178. tasklet_schedule(&cs->event_tasklet);
  179. }
  180. }
  181. spin_unlock_irqrestore(&cs->lock, flags);
  182. }
  183. int gigaset_get_channel(struct bc_state *bcs)
  184. {
  185. unsigned long flags;
  186. spin_lock_irqsave(&bcs->cs->lock, flags);
  187. if (bcs->use_count) {
  188. gig_dbg(DEBUG_ANY, "could not allocate channel %d",
  189. bcs->channel);
  190. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  191. return 0;
  192. }
  193. ++bcs->use_count;
  194. bcs->busy = 1;
  195. gig_dbg(DEBUG_ANY, "allocated channel %d", bcs->channel);
  196. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  197. return 1;
  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_ANY, "could not free channel %d", bcs->channel);
  205. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  206. return;
  207. }
  208. --bcs->use_count;
  209. bcs->busy = 0;
  210. gig_dbg(DEBUG_ANY, "freed channel %d", bcs->channel);
  211. spin_unlock_irqrestore(&bcs->cs->lock, flags);
  212. }
  213. int gigaset_get_channels(struct cardstate *cs)
  214. {
  215. unsigned long flags;
  216. int i;
  217. spin_lock_irqsave(&cs->lock, flags);
  218. for (i = 0; i < cs->channels; ++i)
  219. if (cs->bcs[i].use_count) {
  220. spin_unlock_irqrestore(&cs->lock, flags);
  221. gig_dbg(DEBUG_ANY, "could not allocate all channels");
  222. return 0;
  223. }
  224. for (i = 0; i < cs->channels; ++i)
  225. ++cs->bcs[i].use_count;
  226. spin_unlock_irqrestore(&cs->lock, flags);
  227. gig_dbg(DEBUG_ANY, "allocated all channels");
  228. return 1;
  229. }
  230. void gigaset_free_channels(struct cardstate *cs)
  231. {
  232. unsigned long flags;
  233. int i;
  234. gig_dbg(DEBUG_ANY, "unblocking all channels");
  235. spin_lock_irqsave(&cs->lock, flags);
  236. for (i = 0; i < cs->channels; ++i)
  237. --cs->bcs[i].use_count;
  238. spin_unlock_irqrestore(&cs->lock, flags);
  239. }
  240. void gigaset_block_channels(struct cardstate *cs)
  241. {
  242. unsigned long flags;
  243. int i;
  244. gig_dbg(DEBUG_ANY, "blocking all channels");
  245. spin_lock_irqsave(&cs->lock, flags);
  246. for (i = 0; i < cs->channels; ++i)
  247. ++cs->bcs[i].use_count;
  248. spin_unlock_irqrestore(&cs->lock, flags);
  249. }
  250. static void clear_events(struct cardstate *cs)
  251. {
  252. struct event_t *ev;
  253. unsigned head, tail;
  254. unsigned long flags;
  255. spin_lock_irqsave(&cs->ev_lock, flags);
  256. head = cs->ev_head;
  257. tail = cs->ev_tail;
  258. while (tail != head) {
  259. ev = cs->events + head;
  260. kfree(ev->ptr);
  261. head = (head + 1) % MAX_EVENTS;
  262. }
  263. cs->ev_head = tail;
  264. spin_unlock_irqrestore(&cs->ev_lock, flags);
  265. }
  266. struct event_t *gigaset_add_event(struct cardstate *cs,
  267. struct at_state_t *at_state, int type,
  268. void *ptr, int parameter, void *arg)
  269. {
  270. unsigned long flags;
  271. unsigned next, tail;
  272. struct event_t *event = NULL;
  273. spin_lock_irqsave(&cs->ev_lock, flags);
  274. tail = cs->ev_tail;
  275. next = (tail + 1) % MAX_EVENTS;
  276. if (unlikely(next == cs->ev_head))
  277. err("event queue full");
  278. else {
  279. event = cs->events + tail;
  280. event->type = type;
  281. event->at_state = at_state;
  282. event->cid = -1;
  283. event->ptr = ptr;
  284. event->arg = arg;
  285. event->parameter = parameter;
  286. cs->ev_tail = next;
  287. }
  288. spin_unlock_irqrestore(&cs->ev_lock, flags);
  289. return event;
  290. }
  291. EXPORT_SYMBOL_GPL(gigaset_add_event);
  292. static void free_strings(struct at_state_t *at_state)
  293. {
  294. int i;
  295. for (i = 0; i < STR_NUM; ++i) {
  296. kfree(at_state->str_var[i]);
  297. at_state->str_var[i] = NULL;
  298. }
  299. }
  300. static void clear_at_state(struct at_state_t *at_state)
  301. {
  302. free_strings(at_state);
  303. }
  304. static void dealloc_at_states(struct cardstate *cs)
  305. {
  306. struct at_state_t *cur, *next;
  307. list_for_each_entry_safe(cur, next, &cs->temp_at_states, list) {
  308. list_del(&cur->list);
  309. free_strings(cur);
  310. kfree(cur);
  311. }
  312. }
  313. static void gigaset_freebcs(struct bc_state *bcs)
  314. {
  315. int i;
  316. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->hw", bcs->channel);
  317. if (!bcs->cs->ops->freebcshw(bcs)) {
  318. gig_dbg(DEBUG_INIT, "failed");
  319. }
  320. gig_dbg(DEBUG_INIT, "clearing bcs[%d]->at_state", bcs->channel);
  321. clear_at_state(&bcs->at_state);
  322. gig_dbg(DEBUG_INIT, "freeing bcs[%d]->skb", bcs->channel);
  323. if (bcs->skb)
  324. dev_kfree_skb(bcs->skb);
  325. for (i = 0; i < AT_NUM; ++i) {
  326. kfree(bcs->commands[i]);
  327. bcs->commands[i] = NULL;
  328. }
  329. }
  330. static struct cardstate *alloc_cs(struct gigaset_driver *drv)
  331. {
  332. unsigned long flags;
  333. unsigned i;
  334. static struct cardstate *ret = NULL;
  335. spin_lock_irqsave(&drv->lock, flags);
  336. for (i = 0; i < drv->minors; ++i) {
  337. if (!(drv->flags[i] & VALID_MINOR)) {
  338. drv->flags[i] = VALID_MINOR;
  339. ret = drv->cs + i;
  340. }
  341. if (ret)
  342. break;
  343. }
  344. spin_unlock_irqrestore(&drv->lock, flags);
  345. return ret;
  346. }
  347. static void free_cs(struct cardstate *cs)
  348. {
  349. unsigned long flags;
  350. struct gigaset_driver *drv = cs->driver;
  351. spin_lock_irqsave(&drv->lock, flags);
  352. drv->flags[cs->minor_index] = 0;
  353. spin_unlock_irqrestore(&drv->lock, flags);
  354. }
  355. static void make_valid(struct cardstate *cs, unsigned mask)
  356. {
  357. unsigned long flags;
  358. struct gigaset_driver *drv = cs->driver;
  359. spin_lock_irqsave(&drv->lock, flags);
  360. drv->flags[cs->minor_index] |= mask;
  361. spin_unlock_irqrestore(&drv->lock, flags);
  362. }
  363. static void make_invalid(struct cardstate *cs, unsigned mask)
  364. {
  365. unsigned long flags;
  366. struct gigaset_driver *drv = cs->driver;
  367. spin_lock_irqsave(&drv->lock, flags);
  368. drv->flags[cs->minor_index] &= ~mask;
  369. spin_unlock_irqrestore(&drv->lock, flags);
  370. }
  371. void gigaset_freecs(struct cardstate *cs)
  372. {
  373. int i;
  374. unsigned long flags;
  375. if (!cs)
  376. return;
  377. mutex_lock(&cs->mutex);
  378. if (!cs->bcs)
  379. goto f_cs;
  380. if (!cs->inbuf)
  381. goto f_bcs;
  382. spin_lock_irqsave(&cs->lock, flags);
  383. cs->running = 0;
  384. spin_unlock_irqrestore(&cs->lock, flags); /* event handler and timer are
  385. not rescheduled below */
  386. tasklet_kill(&cs->event_tasklet);
  387. del_timer_sync(&cs->timer);
  388. switch (cs->cs_init) {
  389. default:
  390. gigaset_if_free(cs);
  391. gig_dbg(DEBUG_INIT, "clearing hw");
  392. cs->ops->freecshw(cs);
  393. //FIXME cmdbuf
  394. /* fall through */
  395. case 2: /* error in initcshw */
  396. /* Deregister from LL */
  397. make_invalid(cs, VALID_ID);
  398. gig_dbg(DEBUG_INIT, "clearing iif");
  399. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  400. /* fall through */
  401. case 1: /* error when regestering to LL */
  402. gig_dbg(DEBUG_INIT, "clearing at_state");
  403. clear_at_state(&cs->at_state);
  404. dealloc_at_states(cs);
  405. /* fall through */
  406. case 0: /* error in one call to initbcs */
  407. for (i = 0; i < cs->channels; ++i) {
  408. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  409. gigaset_freebcs(cs->bcs + i);
  410. }
  411. clear_events(cs);
  412. gig_dbg(DEBUG_INIT, "freeing inbuf");
  413. kfree(cs->inbuf);
  414. }
  415. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  416. kfree(cs->bcs);
  417. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  418. mutex_unlock(&cs->mutex);
  419. free_cs(cs);
  420. }
  421. EXPORT_SYMBOL_GPL(gigaset_freecs);
  422. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  423. struct cardstate *cs, int cid)
  424. {
  425. int i;
  426. INIT_LIST_HEAD(&at_state->list);
  427. at_state->waiting = 0;
  428. at_state->getstring = 0;
  429. at_state->pending_commands = 0;
  430. at_state->timer_expires = 0;
  431. at_state->timer_active = 0;
  432. at_state->timer_index = 0;
  433. at_state->seq_index = 0;
  434. at_state->ConState = 0;
  435. for (i = 0; i < STR_NUM; ++i)
  436. at_state->str_var[i] = NULL;
  437. at_state->int_var[VAR_ZDLE] = 0;
  438. at_state->int_var[VAR_ZCTP] = -1;
  439. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  440. at_state->cs = cs;
  441. at_state->bcs = bcs;
  442. at_state->cid = cid;
  443. if (!cid)
  444. at_state->replystruct = cs->tabnocid;
  445. else
  446. at_state->replystruct = cs->tabcid;
  447. }
  448. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  449. struct cardstate *cs, int inputstate)
  450. /* inbuf->read must be allocated before! */
  451. {
  452. atomic_set(&inbuf->head, 0);
  453. atomic_set(&inbuf->tail, 0);
  454. inbuf->cs = cs;
  455. inbuf->bcs = bcs; /*base driver: NULL*/
  456. inbuf->rcvbuf = NULL; //FIXME
  457. inbuf->inputstate = inputstate;
  458. }
  459. /* append received bytes to inbuf */
  460. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  461. unsigned numbytes)
  462. {
  463. unsigned n, head, tail, bytesleft;
  464. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  465. if (!numbytes)
  466. return 0;
  467. bytesleft = numbytes;
  468. tail = atomic_read(&inbuf->tail);
  469. head = atomic_read(&inbuf->head);
  470. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  471. while (bytesleft) {
  472. if (head > tail)
  473. n = head - 1 - tail;
  474. else if (head == 0)
  475. n = (RBUFSIZE-1) - tail;
  476. else
  477. n = RBUFSIZE - tail;
  478. if (!n) {
  479. dev_err(inbuf->cs->dev,
  480. "buffer overflow (%u bytes lost)", bytesleft);
  481. break;
  482. }
  483. if (n > bytesleft)
  484. n = bytesleft;
  485. memcpy(inbuf->data + tail, src, n);
  486. bytesleft -= n;
  487. tail = (tail + n) % RBUFSIZE;
  488. src += n;
  489. }
  490. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  491. atomic_set(&inbuf->tail, tail);
  492. return numbytes != bytesleft;
  493. }
  494. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  495. /* Initialize the b-channel structure */
  496. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  497. struct cardstate *cs, int channel)
  498. {
  499. int i;
  500. bcs->tx_skb = NULL; //FIXME -> hw part
  501. skb_queue_head_init(&bcs->squeue);
  502. bcs->corrupted = 0;
  503. bcs->trans_down = 0;
  504. bcs->trans_up = 0;
  505. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  506. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  507. bcs->rcvbytes = 0;
  508. #ifdef CONFIG_GIGASET_DEBUG
  509. bcs->emptycount = 0;
  510. #endif
  511. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  512. bcs->fcs = PPP_INITFCS;
  513. bcs->inputstate = 0;
  514. if (cs->ignoreframes) {
  515. bcs->inputstate |= INS_skip_frame;
  516. bcs->skb = NULL;
  517. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  518. skb_reserve(bcs->skb, HW_HDR_LEN);
  519. else {
  520. dev_warn(cs->dev, "could not allocate skb\n");
  521. bcs->inputstate |= INS_skip_frame;
  522. }
  523. bcs->channel = channel;
  524. bcs->cs = cs;
  525. bcs->chstate = 0;
  526. bcs->use_count = 1;
  527. bcs->busy = 0;
  528. bcs->ignore = cs->ignoreframes;
  529. for (i = 0; i < AT_NUM; ++i)
  530. bcs->commands[i] = NULL;
  531. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  532. if (cs->ops->initbcshw(bcs))
  533. return bcs;
  534. gig_dbg(DEBUG_INIT, " failed");
  535. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  536. if (bcs->skb)
  537. dev_kfree_skb(bcs->skb);
  538. return NULL;
  539. }
  540. /* gigaset_initcs
  541. * Allocate and initialize cardstate structure for Gigaset driver
  542. * Calls hardware dependent gigaset_initcshw() function
  543. * Calls B channel initialization function gigaset_initbcs() for each B channel
  544. * parameters:
  545. * drv hardware driver the device belongs to
  546. * channels number of B channels supported by device
  547. * onechannel !=0: B channel data and AT commands share one
  548. * communication channel
  549. * ==0: B channels have separate communication channels
  550. * ignoreframes number of frames to ignore after setting up B channel
  551. * cidmode !=0: start in CallID mode
  552. * modulename name of driver module (used for I4L registration)
  553. * return value:
  554. * pointer to cardstate structure
  555. */
  556. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  557. int onechannel, int ignoreframes,
  558. int cidmode, const char *modulename)
  559. {
  560. struct cardstate *cs = NULL;
  561. unsigned long flags;
  562. int i;
  563. gig_dbg(DEBUG_INIT, "allocating cs");
  564. cs = alloc_cs(drv);
  565. if (!cs)
  566. goto error;
  567. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  568. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  569. if (!cs->bcs)
  570. goto error;
  571. gig_dbg(DEBUG_INIT, "allocating inbuf");
  572. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  573. if (!cs->inbuf)
  574. goto error;
  575. cs->cs_init = 0;
  576. cs->channels = channels;
  577. cs->onechannel = onechannel;
  578. cs->ignoreframes = ignoreframes;
  579. INIT_LIST_HEAD(&cs->temp_at_states);
  580. cs->running = 0;
  581. init_timer(&cs->timer); /* clear next & prev */
  582. spin_lock_init(&cs->ev_lock);
  583. cs->ev_tail = 0;
  584. cs->ev_head = 0;
  585. mutex_init(&cs->mutex);
  586. mutex_lock(&cs->mutex);
  587. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  588. (unsigned long) cs);
  589. atomic_set(&cs->commands_pending, 0);
  590. cs->cur_at_seq = 0;
  591. cs->gotfwver = -1;
  592. cs->open_count = 0;
  593. cs->dev = NULL;
  594. cs->tty = NULL;
  595. cs->cidmode = cidmode != 0;
  596. //if(onechannel) { //FIXME
  597. cs->tabnocid = gigaset_tab_nocid_m10x;
  598. cs->tabcid = gigaset_tab_cid_m10x;
  599. //} else {
  600. // cs->tabnocid = gigaset_tab_nocid;
  601. // cs->tabcid = gigaset_tab_cid;
  602. //}
  603. init_waitqueue_head(&cs->waitqueue);
  604. cs->waiting = 0;
  605. atomic_set(&cs->mode, M_UNKNOWN);
  606. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  607. for (i = 0; i < channels; ++i) {
  608. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  609. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  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. if (onechannel) { //FIXME distinction necessary?
  620. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  621. } else
  622. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  623. cs->connected = 0;
  624. cs->isdn_up = 0;
  625. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  626. cs->cmdbuf = cs->lastcmdbuf = NULL;
  627. spin_lock_init(&cs->cmdlock);
  628. cs->curlen = 0;
  629. cs->cmdbytes = 0;
  630. gig_dbg(DEBUG_INIT, "setting up iif");
  631. if (!gigaset_register_to_LL(cs, modulename)) {
  632. err("register_isdn failed");
  633. goto error;
  634. }
  635. make_valid(cs, VALID_ID);
  636. ++cs->cs_init;
  637. gig_dbg(DEBUG_INIT, "setting up hw");
  638. if (!cs->ops->initcshw(cs))
  639. goto error;
  640. ++cs->cs_init;
  641. gigaset_if_init(cs);
  642. spin_lock_irqsave(&cs->lock, flags);
  643. cs->running = 1;
  644. spin_unlock_irqrestore(&cs->lock, flags);
  645. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  646. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  647. /* FIXME: can jiffies increase too much until the timer is added?
  648. * Same problem(?) with mod_timer() in timer_tick(). */
  649. add_timer(&cs->timer);
  650. gig_dbg(DEBUG_INIT, "cs initialized");
  651. mutex_unlock(&cs->mutex);
  652. return cs;
  653. error: if (cs)
  654. mutex_unlock(&cs->mutex);
  655. gig_dbg(DEBUG_INIT, "failed");
  656. gigaset_freecs(cs);
  657. return NULL;
  658. }
  659. EXPORT_SYMBOL_GPL(gigaset_initcs);
  660. /* ReInitialize the b-channel structure */
  661. /* e.g. called on hangup, disconnect */
  662. void gigaset_bcs_reinit(struct bc_state *bcs)
  663. {
  664. struct sk_buff *skb;
  665. struct cardstate *cs = bcs->cs;
  666. unsigned long flags;
  667. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  668. dev_kfree_skb(skb);
  669. spin_lock_irqsave(&cs->lock, flags);
  670. clear_at_state(&bcs->at_state);
  671. bcs->at_state.ConState = 0;
  672. bcs->at_state.timer_active = 0;
  673. bcs->at_state.timer_expires = 0;
  674. bcs->at_state.cid = -1; /* No CID defined */
  675. spin_unlock_irqrestore(&cs->lock, flags);
  676. bcs->inputstate = 0;
  677. #ifdef CONFIG_GIGASET_DEBUG
  678. bcs->emptycount = 0;
  679. #endif
  680. bcs->fcs = PPP_INITFCS;
  681. bcs->chstate = 0;
  682. bcs->ignore = cs->ignoreframes;
  683. if (bcs->ignore)
  684. bcs->inputstate |= INS_skip_frame;
  685. cs->ops->reinitbcshw(bcs);
  686. }
  687. static void cleanup_cs(struct cardstate *cs)
  688. {
  689. struct cmdbuf_t *cb, *tcb;
  690. int i;
  691. unsigned long flags;
  692. spin_lock_irqsave(&cs->lock, flags);
  693. atomic_set(&cs->mode, M_UNKNOWN);
  694. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  695. clear_at_state(&cs->at_state);
  696. dealloc_at_states(cs);
  697. free_strings(&cs->at_state);
  698. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  699. kfree(cs->inbuf->rcvbuf);
  700. cs->inbuf->rcvbuf = NULL;
  701. cs->inbuf->inputstate = INS_command;
  702. atomic_set(&cs->inbuf->head, 0);
  703. atomic_set(&cs->inbuf->tail, 0);
  704. cb = cs->cmdbuf;
  705. while (cb) {
  706. tcb = cb;
  707. cb = cb->next;
  708. kfree(tcb);
  709. }
  710. cs->cmdbuf = cs->lastcmdbuf = NULL;
  711. cs->curlen = 0;
  712. cs->cmdbytes = 0;
  713. cs->gotfwver = -1;
  714. cs->dle = 0;
  715. cs->cur_at_seq = 0;
  716. atomic_set(&cs->commands_pending, 0);
  717. cs->cbytes = 0;
  718. spin_unlock_irqrestore(&cs->lock, flags);
  719. for (i = 0; i < cs->channels; ++i) {
  720. gigaset_freebcs(cs->bcs + i);
  721. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  722. break; //FIXME error handling
  723. }
  724. if (cs->waiting) {
  725. cs->cmd_result = -ENODEV;
  726. cs->waiting = 0;
  727. wake_up_interruptible(&cs->waitqueue);
  728. }
  729. }
  730. int gigaset_start(struct cardstate *cs)
  731. {
  732. unsigned long flags;
  733. if (mutex_lock_interruptible(&cs->mutex))
  734. return 0;
  735. spin_lock_irqsave(&cs->lock, flags);
  736. cs->connected = 1;
  737. spin_unlock_irqrestore(&cs->lock, flags);
  738. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  739. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  740. cs->ops->baud_rate(cs, B115200);
  741. cs->ops->set_line_ctrl(cs, CS8);
  742. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  743. } else {
  744. //FIXME use some saved values?
  745. }
  746. cs->waiting = 1;
  747. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  748. cs->waiting = 0;
  749. //FIXME what should we do?
  750. goto error;
  751. }
  752. gig_dbg(DEBUG_CMD, "scheduling START");
  753. gigaset_schedule_event(cs);
  754. wait_event(cs->waitqueue, !cs->waiting);
  755. /* set up device sysfs */
  756. gigaset_init_dev_sysfs(cs);
  757. mutex_unlock(&cs->mutex);
  758. return 1;
  759. error:
  760. mutex_unlock(&cs->mutex);
  761. return 0;
  762. }
  763. EXPORT_SYMBOL_GPL(gigaset_start);
  764. void gigaset_shutdown(struct cardstate *cs)
  765. {
  766. mutex_lock(&cs->mutex);
  767. cs->waiting = 1;
  768. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  769. //FIXME what should we do?
  770. goto exit;
  771. }
  772. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  773. gigaset_schedule_event(cs);
  774. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  775. warn("%s: aborted", __func__);
  776. //FIXME
  777. }
  778. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  779. //FIXME?
  780. //gigaset_baud_rate(cs, B115200);
  781. //gigaset_set_line_ctrl(cs, CS8);
  782. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  783. //cs->control_state = 0;
  784. } else {
  785. //FIXME use some saved values?
  786. }
  787. cleanup_cs(cs);
  788. exit:
  789. mutex_unlock(&cs->mutex);
  790. }
  791. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  792. void gigaset_stop(struct cardstate *cs)
  793. {
  794. mutex_lock(&cs->mutex);
  795. cs->waiting = 1;
  796. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  797. //FIXME what should we do?
  798. goto exit;
  799. }
  800. gig_dbg(DEBUG_CMD, "scheduling STOP");
  801. gigaset_schedule_event(cs);
  802. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  803. warn("%s: aborted", __func__);
  804. //FIXME
  805. }
  806. /* clear device sysfs */
  807. gigaset_free_dev_sysfs(cs);
  808. cleanup_cs(cs);
  809. exit:
  810. mutex_unlock(&cs->mutex);
  811. }
  812. EXPORT_SYMBOL_GPL(gigaset_stop);
  813. static LIST_HEAD(drivers);
  814. static spinlock_t driver_lock = SPIN_LOCK_UNLOCKED;
  815. struct cardstate *gigaset_get_cs_by_id(int id)
  816. {
  817. unsigned long flags;
  818. static struct cardstate *ret = NULL;
  819. static struct cardstate *cs;
  820. struct gigaset_driver *drv;
  821. unsigned i;
  822. spin_lock_irqsave(&driver_lock, flags);
  823. list_for_each_entry(drv, &drivers, list) {
  824. spin_lock(&drv->lock);
  825. for (i = 0; i < drv->minors; ++i) {
  826. if (drv->flags[i] & VALID_ID) {
  827. cs = drv->cs + i;
  828. if (cs->myid == id)
  829. ret = cs;
  830. }
  831. if (ret)
  832. break;
  833. }
  834. spin_unlock(&drv->lock);
  835. if (ret)
  836. break;
  837. }
  838. spin_unlock_irqrestore(&driver_lock, flags);
  839. return ret;
  840. }
  841. void gigaset_debugdrivers(void)
  842. {
  843. unsigned long flags;
  844. static struct cardstate *cs;
  845. struct gigaset_driver *drv;
  846. unsigned i;
  847. spin_lock_irqsave(&driver_lock, flags);
  848. list_for_each_entry(drv, &drivers, list) {
  849. gig_dbg(DEBUG_DRIVER, "driver %p", drv);
  850. spin_lock(&drv->lock);
  851. for (i = 0; i < drv->minors; ++i) {
  852. gig_dbg(DEBUG_DRIVER, " index %u", i);
  853. gig_dbg(DEBUG_DRIVER, " flags 0x%02x",
  854. drv->flags[i]);
  855. cs = drv->cs + i;
  856. gig_dbg(DEBUG_DRIVER, " cardstate %p", cs);
  857. gig_dbg(DEBUG_DRIVER, " minor_index %u",
  858. cs->minor_index);
  859. gig_dbg(DEBUG_DRIVER, " driver %p", cs->driver);
  860. gig_dbg(DEBUG_DRIVER, " i4l id %d", cs->myid);
  861. }
  862. spin_unlock(&drv->lock);
  863. }
  864. spin_unlock_irqrestore(&driver_lock, flags);
  865. }
  866. EXPORT_SYMBOL_GPL(gigaset_debugdrivers);
  867. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  868. {
  869. if (tty->index < 0 || tty->index >= tty->driver->num)
  870. return NULL;
  871. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  872. }
  873. struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  874. {
  875. unsigned long flags;
  876. static struct cardstate *ret = NULL;
  877. struct gigaset_driver *drv;
  878. unsigned index;
  879. spin_lock_irqsave(&driver_lock, flags);
  880. list_for_each_entry(drv, &drivers, list) {
  881. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  882. continue;
  883. index = minor - drv->minor;
  884. spin_lock(&drv->lock);
  885. if (drv->flags[index] & VALID_MINOR)
  886. ret = drv->cs + index;
  887. spin_unlock(&drv->lock);
  888. if (ret)
  889. break;
  890. }
  891. spin_unlock_irqrestore(&driver_lock, flags);
  892. return ret;
  893. }
  894. void gigaset_freedriver(struct gigaset_driver *drv)
  895. {
  896. unsigned long flags;
  897. spin_lock_irqsave(&driver_lock, flags);
  898. list_del(&drv->list);
  899. spin_unlock_irqrestore(&driver_lock, flags);
  900. gigaset_if_freedriver(drv);
  901. module_put(drv->owner);
  902. kfree(drv->cs);
  903. kfree(drv->flags);
  904. kfree(drv);
  905. }
  906. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  907. /* gigaset_initdriver
  908. * Allocate and initialize gigaset_driver structure. Initialize interface.
  909. * parameters:
  910. * minor First minor number
  911. * minors Number of minors this driver can handle
  912. * procname Name of the driver
  913. * devname Name of the device files (prefix without minor number)
  914. * devfsname Devfs name of the device files without %d
  915. * return value:
  916. * Pointer to the gigaset_driver structure on success, NULL on failure.
  917. */
  918. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  919. const char *procname,
  920. const char *devname,
  921. const char *devfsname,
  922. const struct gigaset_ops *ops,
  923. struct module *owner)
  924. {
  925. struct gigaset_driver *drv;
  926. unsigned long flags;
  927. unsigned i;
  928. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  929. if (!drv)
  930. return NULL;
  931. if (!try_module_get(owner))
  932. return NULL;
  933. drv->cs = NULL;
  934. drv->have_tty = 0;
  935. drv->minor = minor;
  936. drv->minors = minors;
  937. spin_lock_init(&drv->lock);
  938. drv->blocked = 0;
  939. drv->ops = ops;
  940. drv->owner = owner;
  941. INIT_LIST_HEAD(&drv->list);
  942. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  943. if (!drv->cs)
  944. goto out1;
  945. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  946. if (!drv->flags)
  947. goto out2;
  948. for (i = 0; i < minors; ++i) {
  949. drv->flags[i] = 0;
  950. drv->cs[i].driver = drv;
  951. drv->cs[i].ops = drv->ops;
  952. drv->cs[i].minor_index = i;
  953. }
  954. gigaset_if_initdriver(drv, procname, devname, devfsname);
  955. spin_lock_irqsave(&driver_lock, flags);
  956. list_add(&drv->list, &drivers);
  957. spin_unlock_irqrestore(&driver_lock, flags);
  958. return drv;
  959. out2:
  960. kfree(drv->cs);
  961. out1:
  962. kfree(drv);
  963. module_put(owner);
  964. return NULL;
  965. }
  966. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  967. /* For drivers without fixed assignment device<->cardstate (usb) */
  968. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  969. {
  970. unsigned long flags;
  971. struct cardstate *cs = NULL;
  972. unsigned i;
  973. spin_lock_irqsave(&drv->lock, flags);
  974. if (drv->blocked)
  975. goto exit;
  976. for (i = 0; i < drv->minors; ++i) {
  977. if ((drv->flags[i] & VALID_MINOR) &&
  978. !(drv->flags[i] & ASSIGNED)) {
  979. drv->flags[i] |= ASSIGNED;
  980. cs = drv->cs + i;
  981. break;
  982. }
  983. }
  984. exit:
  985. spin_unlock_irqrestore(&drv->lock, flags);
  986. return cs;
  987. }
  988. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  989. void gigaset_unassign(struct cardstate *cs)
  990. {
  991. unsigned long flags;
  992. unsigned *minor_flags;
  993. struct gigaset_driver *drv;
  994. if (!cs)
  995. return;
  996. drv = cs->driver;
  997. spin_lock_irqsave(&drv->lock, flags);
  998. minor_flags = drv->flags + cs->minor_index;
  999. if (*minor_flags & VALID_MINOR)
  1000. *minor_flags &= ~ASSIGNED;
  1001. spin_unlock_irqrestore(&drv->lock, flags);
  1002. }
  1003. EXPORT_SYMBOL_GPL(gigaset_unassign);
  1004. void gigaset_blockdriver(struct gigaset_driver *drv)
  1005. {
  1006. unsigned long flags;
  1007. spin_lock_irqsave(&drv->lock, flags);
  1008. drv->blocked = 1;
  1009. spin_unlock_irqrestore(&drv->lock, flags);
  1010. }
  1011. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  1012. static int __init gigaset_init_module(void)
  1013. {
  1014. /* in accordance with the principle of least astonishment,
  1015. * setting the 'debug' parameter to 1 activates a sensible
  1016. * set of default debug levels
  1017. */
  1018. if (gigaset_debuglevel == 1)
  1019. gigaset_debuglevel = DEBUG_DEFAULT;
  1020. info(DRIVER_AUTHOR);
  1021. info(DRIVER_DESC);
  1022. return 0;
  1023. }
  1024. static void __exit gigaset_exit_module(void)
  1025. {
  1026. }
  1027. module_init(gigaset_init_module);
  1028. module_exit(gigaset_exit_module);
  1029. MODULE_AUTHOR(DRIVER_AUTHOR);
  1030. MODULE_DESCRIPTION(DRIVER_DESC);
  1031. MODULE_LICENSE("GPL");