common.c 28 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. /* clear device sysfs */
  391. gigaset_free_dev_sysfs(cs);
  392. gigaset_if_free(cs);
  393. gig_dbg(DEBUG_INIT, "clearing hw");
  394. cs->ops->freecshw(cs);
  395. //FIXME cmdbuf
  396. /* fall through */
  397. case 2: /* error in initcshw */
  398. /* Deregister from LL */
  399. make_invalid(cs, VALID_ID);
  400. gig_dbg(DEBUG_INIT, "clearing iif");
  401. gigaset_i4l_cmd(cs, ISDN_STAT_UNLOAD);
  402. /* fall through */
  403. case 1: /* error when regestering to LL */
  404. gig_dbg(DEBUG_INIT, "clearing at_state");
  405. clear_at_state(&cs->at_state);
  406. dealloc_at_states(cs);
  407. /* fall through */
  408. case 0: /* error in one call to initbcs */
  409. for (i = 0; i < cs->channels; ++i) {
  410. gig_dbg(DEBUG_INIT, "clearing bcs[%d]", i);
  411. gigaset_freebcs(cs->bcs + i);
  412. }
  413. clear_events(cs);
  414. gig_dbg(DEBUG_INIT, "freeing inbuf");
  415. kfree(cs->inbuf);
  416. }
  417. f_bcs: gig_dbg(DEBUG_INIT, "freeing bcs[]");
  418. kfree(cs->bcs);
  419. f_cs: gig_dbg(DEBUG_INIT, "freeing cs");
  420. mutex_unlock(&cs->mutex);
  421. free_cs(cs);
  422. }
  423. EXPORT_SYMBOL_GPL(gigaset_freecs);
  424. void gigaset_at_init(struct at_state_t *at_state, struct bc_state *bcs,
  425. struct cardstate *cs, int cid)
  426. {
  427. int i;
  428. INIT_LIST_HEAD(&at_state->list);
  429. at_state->waiting = 0;
  430. at_state->getstring = 0;
  431. at_state->pending_commands = 0;
  432. at_state->timer_expires = 0;
  433. at_state->timer_active = 0;
  434. at_state->timer_index = 0;
  435. at_state->seq_index = 0;
  436. at_state->ConState = 0;
  437. for (i = 0; i < STR_NUM; ++i)
  438. at_state->str_var[i] = NULL;
  439. at_state->int_var[VAR_ZDLE] = 0;
  440. at_state->int_var[VAR_ZCTP] = -1;
  441. at_state->int_var[VAR_ZSAU] = ZSAU_NULL;
  442. at_state->cs = cs;
  443. at_state->bcs = bcs;
  444. at_state->cid = cid;
  445. if (!cid)
  446. at_state->replystruct = cs->tabnocid;
  447. else
  448. at_state->replystruct = cs->tabcid;
  449. }
  450. static void gigaset_inbuf_init(struct inbuf_t *inbuf, struct bc_state *bcs,
  451. struct cardstate *cs, int inputstate)
  452. /* inbuf->read must be allocated before! */
  453. {
  454. atomic_set(&inbuf->head, 0);
  455. atomic_set(&inbuf->tail, 0);
  456. inbuf->cs = cs;
  457. inbuf->bcs = bcs; /*base driver: NULL*/
  458. inbuf->rcvbuf = NULL; //FIXME
  459. inbuf->inputstate = inputstate;
  460. }
  461. /* append received bytes to inbuf */
  462. int gigaset_fill_inbuf(struct inbuf_t *inbuf, const unsigned char *src,
  463. unsigned numbytes)
  464. {
  465. unsigned n, head, tail, bytesleft;
  466. gig_dbg(DEBUG_INTR, "received %u bytes", numbytes);
  467. if (!numbytes)
  468. return 0;
  469. bytesleft = numbytes;
  470. tail = atomic_read(&inbuf->tail);
  471. head = atomic_read(&inbuf->head);
  472. gig_dbg(DEBUG_INTR, "buffer state: %u -> %u", head, tail);
  473. while (bytesleft) {
  474. if (head > tail)
  475. n = head - 1 - tail;
  476. else if (head == 0)
  477. n = (RBUFSIZE-1) - tail;
  478. else
  479. n = RBUFSIZE - tail;
  480. if (!n) {
  481. dev_err(inbuf->cs->dev,
  482. "buffer overflow (%u bytes lost)", bytesleft);
  483. break;
  484. }
  485. if (n > bytesleft)
  486. n = bytesleft;
  487. memcpy(inbuf->data + tail, src, n);
  488. bytesleft -= n;
  489. tail = (tail + n) % RBUFSIZE;
  490. src += n;
  491. }
  492. gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
  493. atomic_set(&inbuf->tail, tail);
  494. return numbytes != bytesleft;
  495. }
  496. EXPORT_SYMBOL_GPL(gigaset_fill_inbuf);
  497. /* Initialize the b-channel structure */
  498. static struct bc_state *gigaset_initbcs(struct bc_state *bcs,
  499. struct cardstate *cs, int channel)
  500. {
  501. int i;
  502. bcs->tx_skb = NULL; //FIXME -> hw part
  503. skb_queue_head_init(&bcs->squeue);
  504. bcs->corrupted = 0;
  505. bcs->trans_down = 0;
  506. bcs->trans_up = 0;
  507. gig_dbg(DEBUG_INIT, "setting up bcs[%d]->at_state", channel);
  508. gigaset_at_init(&bcs->at_state, bcs, cs, -1);
  509. bcs->rcvbytes = 0;
  510. #ifdef CONFIG_GIGASET_DEBUG
  511. bcs->emptycount = 0;
  512. #endif
  513. gig_dbg(DEBUG_INIT, "allocating bcs[%d]->skb", channel);
  514. bcs->fcs = PPP_INITFCS;
  515. bcs->inputstate = 0;
  516. if (cs->ignoreframes) {
  517. bcs->inputstate |= INS_skip_frame;
  518. bcs->skb = NULL;
  519. } else if ((bcs->skb = dev_alloc_skb(SBUFSIZE + HW_HDR_LEN)) != NULL)
  520. skb_reserve(bcs->skb, HW_HDR_LEN);
  521. else {
  522. dev_warn(cs->dev, "could not allocate skb\n");
  523. bcs->inputstate |= INS_skip_frame;
  524. }
  525. bcs->channel = channel;
  526. bcs->cs = cs;
  527. bcs->chstate = 0;
  528. bcs->use_count = 1;
  529. bcs->busy = 0;
  530. bcs->ignore = cs->ignoreframes;
  531. for (i = 0; i < AT_NUM; ++i)
  532. bcs->commands[i] = NULL;
  533. gig_dbg(DEBUG_INIT, " setting up bcs[%d]->hw", channel);
  534. if (cs->ops->initbcshw(bcs))
  535. return bcs;
  536. gig_dbg(DEBUG_INIT, " failed");
  537. gig_dbg(DEBUG_INIT, " freeing bcs[%d]->skb", channel);
  538. if (bcs->skb)
  539. dev_kfree_skb(bcs->skb);
  540. return NULL;
  541. }
  542. /* gigaset_initcs
  543. * Allocate and initialize cardstate structure for Gigaset driver
  544. * Calls hardware dependent gigaset_initcshw() function
  545. * Calls B channel initialization function gigaset_initbcs() for each B channel
  546. * parameters:
  547. * drv hardware driver the device belongs to
  548. * channels number of B channels supported by device
  549. * onechannel !=0: B channel data and AT commands share one
  550. * communication channel
  551. * ==0: B channels have separate communication channels
  552. * ignoreframes number of frames to ignore after setting up B channel
  553. * cidmode !=0: start in CallID mode
  554. * modulename name of driver module (used for I4L registration)
  555. * return value:
  556. * pointer to cardstate structure
  557. */
  558. struct cardstate *gigaset_initcs(struct gigaset_driver *drv, int channels,
  559. int onechannel, int ignoreframes,
  560. int cidmode, const char *modulename)
  561. {
  562. struct cardstate *cs = NULL;
  563. unsigned long flags;
  564. int i;
  565. gig_dbg(DEBUG_INIT, "allocating cs");
  566. cs = alloc_cs(drv);
  567. if (!cs)
  568. goto error;
  569. gig_dbg(DEBUG_INIT, "allocating bcs[0..%d]", channels - 1);
  570. cs->bcs = kmalloc(channels * sizeof(struct bc_state), GFP_KERNEL);
  571. if (!cs->bcs)
  572. goto error;
  573. gig_dbg(DEBUG_INIT, "allocating inbuf");
  574. cs->inbuf = kmalloc(sizeof(struct inbuf_t), GFP_KERNEL);
  575. if (!cs->inbuf)
  576. goto error;
  577. cs->cs_init = 0;
  578. cs->channels = channels;
  579. cs->onechannel = onechannel;
  580. cs->ignoreframes = ignoreframes;
  581. INIT_LIST_HEAD(&cs->temp_at_states);
  582. cs->running = 0;
  583. init_timer(&cs->timer); /* clear next & prev */
  584. spin_lock_init(&cs->ev_lock);
  585. cs->ev_tail = 0;
  586. cs->ev_head = 0;
  587. mutex_init(&cs->mutex);
  588. mutex_lock(&cs->mutex);
  589. tasklet_init(&cs->event_tasklet, &gigaset_handle_event,
  590. (unsigned long) cs);
  591. atomic_set(&cs->commands_pending, 0);
  592. cs->cur_at_seq = 0;
  593. cs->gotfwver = -1;
  594. cs->open_count = 0;
  595. cs->dev = NULL;
  596. cs->tty = NULL;
  597. cs->class = NULL;
  598. cs->cidmode = cidmode != 0;
  599. //if(onechannel) { //FIXME
  600. cs->tabnocid = gigaset_tab_nocid_m10x;
  601. cs->tabcid = gigaset_tab_cid_m10x;
  602. //} else {
  603. // cs->tabnocid = gigaset_tab_nocid;
  604. // cs->tabcid = gigaset_tab_cid;
  605. //}
  606. init_waitqueue_head(&cs->waitqueue);
  607. cs->waiting = 0;
  608. atomic_set(&cs->mode, M_UNKNOWN);
  609. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  610. for (i = 0; i < channels; ++i) {
  611. gig_dbg(DEBUG_INIT, "setting up bcs[%d].read", i);
  612. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  613. goto error;
  614. }
  615. ++cs->cs_init;
  616. gig_dbg(DEBUG_INIT, "setting up at_state");
  617. spin_lock_init(&cs->lock);
  618. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  619. cs->dle = 0;
  620. cs->cbytes = 0;
  621. gig_dbg(DEBUG_INIT, "setting up inbuf");
  622. if (onechannel) { //FIXME distinction necessary?
  623. gigaset_inbuf_init(cs->inbuf, cs->bcs, cs, INS_command);
  624. } else
  625. gigaset_inbuf_init(cs->inbuf, NULL, cs, INS_command);
  626. cs->connected = 0;
  627. cs->isdn_up = 0;
  628. gig_dbg(DEBUG_INIT, "setting up cmdbuf");
  629. cs->cmdbuf = cs->lastcmdbuf = NULL;
  630. spin_lock_init(&cs->cmdlock);
  631. cs->curlen = 0;
  632. cs->cmdbytes = 0;
  633. gig_dbg(DEBUG_INIT, "setting up iif");
  634. if (!gigaset_register_to_LL(cs, modulename)) {
  635. err("register_isdn failed");
  636. goto error;
  637. }
  638. make_valid(cs, VALID_ID);
  639. ++cs->cs_init;
  640. gig_dbg(DEBUG_INIT, "setting up hw");
  641. if (!cs->ops->initcshw(cs))
  642. goto error;
  643. ++cs->cs_init;
  644. gigaset_if_init(cs);
  645. /* set up device sysfs */
  646. gigaset_init_dev_sysfs(cs);
  647. spin_lock_irqsave(&cs->lock, flags);
  648. cs->running = 1;
  649. spin_unlock_irqrestore(&cs->lock, flags);
  650. setup_timer(&cs->timer, timer_tick, (unsigned long) cs);
  651. cs->timer.expires = jiffies + msecs_to_jiffies(GIG_TICK);
  652. /* FIXME: can jiffies increase too much until the timer is added?
  653. * Same problem(?) with mod_timer() in timer_tick(). */
  654. add_timer(&cs->timer);
  655. gig_dbg(DEBUG_INIT, "cs initialized");
  656. mutex_unlock(&cs->mutex);
  657. return cs;
  658. error: if (cs)
  659. mutex_unlock(&cs->mutex);
  660. gig_dbg(DEBUG_INIT, "failed");
  661. gigaset_freecs(cs);
  662. return NULL;
  663. }
  664. EXPORT_SYMBOL_GPL(gigaset_initcs);
  665. /* ReInitialize the b-channel structure on hangup */
  666. void gigaset_bcs_reinit(struct bc_state *bcs)
  667. {
  668. struct sk_buff *skb;
  669. struct cardstate *cs = bcs->cs;
  670. unsigned long flags;
  671. while ((skb = skb_dequeue(&bcs->squeue)) != NULL)
  672. dev_kfree_skb(skb);
  673. spin_lock_irqsave(&cs->lock, flags);
  674. clear_at_state(&bcs->at_state);
  675. bcs->at_state.ConState = 0;
  676. bcs->at_state.timer_active = 0;
  677. bcs->at_state.timer_expires = 0;
  678. bcs->at_state.cid = -1; /* No CID defined */
  679. spin_unlock_irqrestore(&cs->lock, flags);
  680. bcs->inputstate = 0;
  681. #ifdef CONFIG_GIGASET_DEBUG
  682. bcs->emptycount = 0;
  683. #endif
  684. bcs->fcs = PPP_INITFCS;
  685. bcs->chstate = 0;
  686. bcs->ignore = cs->ignoreframes;
  687. if (bcs->ignore)
  688. bcs->inputstate |= INS_skip_frame;
  689. cs->ops->reinitbcshw(bcs);
  690. }
  691. static void cleanup_cs(struct cardstate *cs)
  692. {
  693. struct cmdbuf_t *cb, *tcb;
  694. int i;
  695. unsigned long flags;
  696. spin_lock_irqsave(&cs->lock, flags);
  697. atomic_set(&cs->mode, M_UNKNOWN);
  698. atomic_set(&cs->mstate, MS_UNINITIALIZED);
  699. clear_at_state(&cs->at_state);
  700. dealloc_at_states(cs);
  701. free_strings(&cs->at_state);
  702. gigaset_at_init(&cs->at_state, NULL, cs, 0);
  703. kfree(cs->inbuf->rcvbuf);
  704. cs->inbuf->rcvbuf = NULL;
  705. cs->inbuf->inputstate = INS_command;
  706. atomic_set(&cs->inbuf->head, 0);
  707. atomic_set(&cs->inbuf->tail, 0);
  708. cb = cs->cmdbuf;
  709. while (cb) {
  710. tcb = cb;
  711. cb = cb->next;
  712. kfree(tcb);
  713. }
  714. cs->cmdbuf = cs->lastcmdbuf = NULL;
  715. cs->curlen = 0;
  716. cs->cmdbytes = 0;
  717. cs->gotfwver = -1;
  718. cs->dle = 0;
  719. cs->cur_at_seq = 0;
  720. atomic_set(&cs->commands_pending, 0);
  721. cs->cbytes = 0;
  722. spin_unlock_irqrestore(&cs->lock, flags);
  723. for (i = 0; i < cs->channels; ++i) {
  724. gigaset_freebcs(cs->bcs + i);
  725. if (!gigaset_initbcs(cs->bcs + i, cs, i))
  726. break; //FIXME error handling
  727. }
  728. if (cs->waiting) {
  729. cs->cmd_result = -ENODEV;
  730. cs->waiting = 0;
  731. wake_up_interruptible(&cs->waitqueue);
  732. }
  733. }
  734. int gigaset_start(struct cardstate *cs)
  735. {
  736. unsigned long flags;
  737. if (mutex_lock_interruptible(&cs->mutex))
  738. return 0;
  739. spin_lock_irqsave(&cs->lock, flags);
  740. cs->connected = 1;
  741. spin_unlock_irqrestore(&cs->lock, flags);
  742. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  743. cs->ops->set_modem_ctrl(cs, 0, TIOCM_DTR|TIOCM_RTS);
  744. cs->ops->baud_rate(cs, B115200);
  745. cs->ops->set_line_ctrl(cs, CS8);
  746. cs->control_state = TIOCM_DTR|TIOCM_RTS;
  747. } else {
  748. //FIXME use some saved values?
  749. }
  750. cs->waiting = 1;
  751. if (!gigaset_add_event(cs, &cs->at_state, EV_START, NULL, 0, NULL)) {
  752. cs->waiting = 0;
  753. //FIXME what should we do?
  754. goto error;
  755. }
  756. gig_dbg(DEBUG_CMD, "scheduling START");
  757. gigaset_schedule_event(cs);
  758. wait_event(cs->waitqueue, !cs->waiting);
  759. mutex_unlock(&cs->mutex);
  760. return 1;
  761. error:
  762. mutex_unlock(&cs->mutex);
  763. return 0;
  764. }
  765. EXPORT_SYMBOL_GPL(gigaset_start);
  766. void gigaset_shutdown(struct cardstate *cs)
  767. {
  768. mutex_lock(&cs->mutex);
  769. cs->waiting = 1;
  770. if (!gigaset_add_event(cs, &cs->at_state, EV_SHUTDOWN, NULL, 0, NULL)) {
  771. //FIXME what should we do?
  772. goto exit;
  773. }
  774. gig_dbg(DEBUG_CMD, "scheduling SHUTDOWN");
  775. gigaset_schedule_event(cs);
  776. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  777. warn("%s: aborted", __func__);
  778. //FIXME
  779. }
  780. if (atomic_read(&cs->mstate) != MS_LOCKED) {
  781. //FIXME?
  782. //gigaset_baud_rate(cs, B115200);
  783. //gigaset_set_line_ctrl(cs, CS8);
  784. //gigaset_set_modem_ctrl(cs, TIOCM_DTR|TIOCM_RTS, 0);
  785. //cs->control_state = 0;
  786. } else {
  787. //FIXME use some saved values?
  788. }
  789. cleanup_cs(cs);
  790. exit:
  791. mutex_unlock(&cs->mutex);
  792. }
  793. EXPORT_SYMBOL_GPL(gigaset_shutdown);
  794. void gigaset_stop(struct cardstate *cs)
  795. {
  796. mutex_lock(&cs->mutex);
  797. cs->waiting = 1;
  798. if (!gigaset_add_event(cs, &cs->at_state, EV_STOP, NULL, 0, NULL)) {
  799. //FIXME what should we do?
  800. goto exit;
  801. }
  802. gig_dbg(DEBUG_CMD, "scheduling STOP");
  803. gigaset_schedule_event(cs);
  804. if (wait_event_interruptible(cs->waitqueue, !cs->waiting)) {
  805. warn("%s: aborted", __func__);
  806. //FIXME
  807. }
  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 DEFINE_SPINLOCK(driver_lock);
  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. static struct cardstate *gigaset_get_cs_by_minor(unsigned minor)
  867. {
  868. unsigned long flags;
  869. static struct cardstate *ret = NULL;
  870. struct gigaset_driver *drv;
  871. unsigned index;
  872. spin_lock_irqsave(&driver_lock, flags);
  873. list_for_each_entry(drv, &drivers, list) {
  874. if (minor < drv->minor || minor >= drv->minor + drv->minors)
  875. continue;
  876. index = minor - drv->minor;
  877. spin_lock(&drv->lock);
  878. if (drv->flags[index] & VALID_MINOR)
  879. ret = drv->cs + index;
  880. spin_unlock(&drv->lock);
  881. if (ret)
  882. break;
  883. }
  884. spin_unlock_irqrestore(&driver_lock, flags);
  885. return ret;
  886. }
  887. struct cardstate *gigaset_get_cs_by_tty(struct tty_struct *tty)
  888. {
  889. if (tty->index < 0 || tty->index >= tty->driver->num)
  890. return NULL;
  891. return gigaset_get_cs_by_minor(tty->index + tty->driver->minor_start);
  892. }
  893. void gigaset_freedriver(struct gigaset_driver *drv)
  894. {
  895. unsigned long flags;
  896. spin_lock_irqsave(&driver_lock, flags);
  897. list_del(&drv->list);
  898. spin_unlock_irqrestore(&driver_lock, flags);
  899. gigaset_if_freedriver(drv);
  900. module_put(drv->owner);
  901. kfree(drv->cs);
  902. kfree(drv->flags);
  903. kfree(drv);
  904. }
  905. EXPORT_SYMBOL_GPL(gigaset_freedriver);
  906. /* gigaset_initdriver
  907. * Allocate and initialize gigaset_driver structure. Initialize interface.
  908. * parameters:
  909. * minor First minor number
  910. * minors Number of minors this driver can handle
  911. * procname Name of the driver
  912. * devname Name of the device files (prefix without minor number)
  913. * return value:
  914. * Pointer to the gigaset_driver structure on success, NULL on failure.
  915. */
  916. struct gigaset_driver *gigaset_initdriver(unsigned minor, unsigned minors,
  917. const char *procname,
  918. const char *devname,
  919. const struct gigaset_ops *ops,
  920. struct module *owner)
  921. {
  922. struct gigaset_driver *drv;
  923. unsigned long flags;
  924. unsigned i;
  925. drv = kmalloc(sizeof *drv, GFP_KERNEL);
  926. if (!drv)
  927. return NULL;
  928. if (!try_module_get(owner))
  929. goto out1;
  930. drv->cs = NULL;
  931. drv->have_tty = 0;
  932. drv->minor = minor;
  933. drv->minors = minors;
  934. spin_lock_init(&drv->lock);
  935. drv->blocked = 0;
  936. drv->ops = ops;
  937. drv->owner = owner;
  938. INIT_LIST_HEAD(&drv->list);
  939. drv->cs = kmalloc(minors * sizeof *drv->cs, GFP_KERNEL);
  940. if (!drv->cs)
  941. goto out2;
  942. drv->flags = kmalloc(minors * sizeof *drv->flags, GFP_KERNEL);
  943. if (!drv->flags)
  944. goto out3;
  945. for (i = 0; i < minors; ++i) {
  946. drv->flags[i] = 0;
  947. drv->cs[i].driver = drv;
  948. drv->cs[i].ops = drv->ops;
  949. drv->cs[i].minor_index = i;
  950. }
  951. gigaset_if_initdriver(drv, procname, devname);
  952. spin_lock_irqsave(&driver_lock, flags);
  953. list_add(&drv->list, &drivers);
  954. spin_unlock_irqrestore(&driver_lock, flags);
  955. return drv;
  956. out3:
  957. kfree(drv->cs);
  958. out2:
  959. module_put(owner);
  960. out1:
  961. kfree(drv);
  962. return NULL;
  963. }
  964. EXPORT_SYMBOL_GPL(gigaset_initdriver);
  965. /* For drivers without fixed assignment device<->cardstate (usb) */
  966. struct cardstate *gigaset_getunassignedcs(struct gigaset_driver *drv)
  967. {
  968. unsigned long flags;
  969. struct cardstate *cs = NULL;
  970. unsigned i;
  971. spin_lock_irqsave(&drv->lock, flags);
  972. if (drv->blocked)
  973. goto exit;
  974. for (i = 0; i < drv->minors; ++i) {
  975. if ((drv->flags[i] & VALID_MINOR) &&
  976. !(drv->flags[i] & ASSIGNED)) {
  977. drv->flags[i] |= ASSIGNED;
  978. cs = drv->cs + i;
  979. break;
  980. }
  981. }
  982. exit:
  983. spin_unlock_irqrestore(&drv->lock, flags);
  984. return cs;
  985. }
  986. EXPORT_SYMBOL_GPL(gigaset_getunassignedcs);
  987. void gigaset_unassign(struct cardstate *cs)
  988. {
  989. unsigned long flags;
  990. unsigned *minor_flags;
  991. struct gigaset_driver *drv;
  992. if (!cs)
  993. return;
  994. drv = cs->driver;
  995. spin_lock_irqsave(&drv->lock, flags);
  996. minor_flags = drv->flags + cs->minor_index;
  997. if (*minor_flags & VALID_MINOR)
  998. *minor_flags &= ~ASSIGNED;
  999. spin_unlock_irqrestore(&drv->lock, flags);
  1000. }
  1001. EXPORT_SYMBOL_GPL(gigaset_unassign);
  1002. void gigaset_blockdriver(struct gigaset_driver *drv)
  1003. {
  1004. unsigned long flags;
  1005. spin_lock_irqsave(&drv->lock, flags);
  1006. drv->blocked = 1;
  1007. spin_unlock_irqrestore(&drv->lock, flags);
  1008. }
  1009. EXPORT_SYMBOL_GPL(gigaset_blockdriver);
  1010. static int __init gigaset_init_module(void)
  1011. {
  1012. /* in accordance with the principle of least astonishment,
  1013. * setting the 'debug' parameter to 1 activates a sensible
  1014. * set of default debug levels
  1015. */
  1016. if (gigaset_debuglevel == 1)
  1017. gigaset_debuglevel = DEBUG_DEFAULT;
  1018. info(DRIVER_AUTHOR);
  1019. info(DRIVER_DESC);
  1020. return 0;
  1021. }
  1022. static void __exit gigaset_exit_module(void)
  1023. {
  1024. }
  1025. module_init(gigaset_init_module);
  1026. module_exit(gigaset_exit_module);
  1027. MODULE_AUTHOR(DRIVER_AUTHOR);
  1028. MODULE_DESCRIPTION(DRIVER_DESC);
  1029. MODULE_LICENSE("GPL");