bas-gigaset.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566
  1. /*
  2. * USB driver for Gigaset 307x base via direct USB connection.
  3. *
  4. * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
  5. * Tilman Schmidt <tilman@imap.cc>,
  6. * Stefan Eilers.
  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/usb.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. /* Version Information */
  20. #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
  21. #define DRIVER_DESC "USB Driver for Gigaset 307x"
  22. /* Module parameters */
  23. static int startmode = SM_ISDN;
  24. static int cidmode = 1;
  25. module_param(startmode, int, S_IRUGO);
  26. module_param(cidmode, int, S_IRUGO);
  27. MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
  28. MODULE_PARM_DESC(cidmode, "Call-ID mode");
  29. #define GIGASET_MINORS 1
  30. #define GIGASET_MINOR 16
  31. #define GIGASET_MODULENAME "bas_gigaset"
  32. #define GIGASET_DEVNAME "ttyGB"
  33. /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
  34. #define IF_WRITEBUF 264
  35. /* interrupt pipe message size according to ibid. ch. 2.2 */
  36. #define IP_MSGSIZE 3
  37. /* Values for the Gigaset 307x */
  38. #define USB_GIGA_VENDOR_ID 0x0681
  39. #define USB_3070_PRODUCT_ID 0x0001
  40. #define USB_3075_PRODUCT_ID 0x0002
  41. #define USB_SX303_PRODUCT_ID 0x0021
  42. #define USB_SX353_PRODUCT_ID 0x0022
  43. /* table of devices that work with this driver */
  44. static const struct usb_device_id gigaset_table[] = {
  45. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
  46. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
  47. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
  48. { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
  49. { } /* Terminating entry */
  50. };
  51. MODULE_DEVICE_TABLE(usb, gigaset_table);
  52. /*======================= local function prototypes ==========================*/
  53. /* function called if a new device belonging to this driver is connected */
  54. static int gigaset_probe(struct usb_interface *interface,
  55. const struct usb_device_id *id);
  56. /* Function will be called if the device is unplugged */
  57. static void gigaset_disconnect(struct usb_interface *interface);
  58. /* functions called before/after suspend */
  59. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
  60. static int gigaset_resume(struct usb_interface *intf);
  61. /* functions called before/after device reset */
  62. static int gigaset_pre_reset(struct usb_interface *intf);
  63. static int gigaset_post_reset(struct usb_interface *intf);
  64. static int atread_submit(struct cardstate *, int);
  65. static void stopurbs(struct bas_bc_state *);
  66. static int req_submit(struct bc_state *, int, int, int);
  67. static int atwrite_submit(struct cardstate *, unsigned char *, int);
  68. static int start_cbsend(struct cardstate *);
  69. /*============================================================================*/
  70. struct bas_cardstate {
  71. struct usb_device *udev; /* USB device pointer */
  72. struct usb_interface *interface; /* interface for this device */
  73. unsigned char minor; /* starting minor number */
  74. struct urb *urb_ctrl; /* control pipe default URB */
  75. struct usb_ctrlrequest dr_ctrl;
  76. struct timer_list timer_ctrl; /* control request timeout */
  77. int retry_ctrl;
  78. struct timer_list timer_atrdy; /* AT command ready timeout */
  79. struct urb *urb_cmd_out; /* for sending AT commands */
  80. struct usb_ctrlrequest dr_cmd_out;
  81. int retry_cmd_out;
  82. struct urb *urb_cmd_in; /* for receiving AT replies */
  83. struct usb_ctrlrequest dr_cmd_in;
  84. struct timer_list timer_cmd_in; /* receive request timeout */
  85. unsigned char *rcvbuf; /* AT reply receive buffer */
  86. struct urb *urb_int_in; /* URB for interrupt pipe */
  87. unsigned char *int_in_buf;
  88. spinlock_t lock; /* locks all following */
  89. int basstate; /* bitmap (BS_*) */
  90. int pending; /* uncompleted base request */
  91. wait_queue_head_t waitqueue;
  92. int rcvbuf_size; /* size of AT receive buffer */
  93. /* 0: no receive in progress */
  94. int retry_cmd_in; /* receive req retry count */
  95. };
  96. /* status of direct USB connection to 307x base (bits in basstate) */
  97. #define BS_ATOPEN 0x001 /* AT channel open */
  98. #define BS_B1OPEN 0x002 /* B channel 1 open */
  99. #define BS_B2OPEN 0x004 /* B channel 2 open */
  100. #define BS_ATREADY 0x008 /* base ready for AT command */
  101. #define BS_INIT 0x010 /* base has signalled INIT_OK */
  102. #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
  103. #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
  104. #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
  105. #define BS_SUSPEND 0x100 /* USB port suspended */
  106. #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
  107. static struct gigaset_driver *driver;
  108. /* usb specific object needed to register this driver with the usb subsystem */
  109. static struct usb_driver gigaset_usb_driver = {
  110. .name = GIGASET_MODULENAME,
  111. .probe = gigaset_probe,
  112. .disconnect = gigaset_disconnect,
  113. .id_table = gigaset_table,
  114. .suspend = gigaset_suspend,
  115. .resume = gigaset_resume,
  116. .reset_resume = gigaset_post_reset,
  117. .pre_reset = gigaset_pre_reset,
  118. .post_reset = gigaset_post_reset,
  119. };
  120. /* get message text for usb_submit_urb return code
  121. */
  122. static char *get_usb_rcmsg(int rc)
  123. {
  124. static char unkmsg[28];
  125. switch (rc) {
  126. case 0:
  127. return "success";
  128. case -ENOMEM:
  129. return "out of memory";
  130. case -ENODEV:
  131. return "device not present";
  132. case -ENOENT:
  133. return "endpoint not present";
  134. case -ENXIO:
  135. return "URB type not supported";
  136. case -EINVAL:
  137. return "invalid argument";
  138. case -EAGAIN:
  139. return "start frame too early or too much scheduled";
  140. case -EFBIG:
  141. return "too many isochronous frames requested";
  142. case -EPIPE:
  143. return "endpoint stalled";
  144. case -EMSGSIZE:
  145. return "invalid packet size";
  146. case -ENOSPC:
  147. return "would overcommit USB bandwidth";
  148. case -ESHUTDOWN:
  149. return "device shut down";
  150. case -EPERM:
  151. return "reject flag set";
  152. case -EHOSTUNREACH:
  153. return "device suspended";
  154. default:
  155. snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
  156. return unkmsg;
  157. }
  158. }
  159. /* get message text for USB status code
  160. */
  161. static char *get_usb_statmsg(int status)
  162. {
  163. static char unkmsg[28];
  164. switch (status) {
  165. case 0:
  166. return "success";
  167. case -ENOENT:
  168. return "unlinked (sync)";
  169. case -EINPROGRESS:
  170. return "pending";
  171. case -EPROTO:
  172. return "bit stuffing error, timeout, or unknown USB error";
  173. case -EILSEQ:
  174. return "CRC mismatch, timeout, or unknown USB error";
  175. case -ETIME:
  176. return "timed out";
  177. case -EPIPE:
  178. return "endpoint stalled";
  179. case -ECOMM:
  180. return "IN buffer overrun";
  181. case -ENOSR:
  182. return "OUT buffer underrun";
  183. case -EOVERFLOW:
  184. return "too much data";
  185. case -EREMOTEIO:
  186. return "short packet detected";
  187. case -ENODEV:
  188. return "device removed";
  189. case -EXDEV:
  190. return "partial isochronous transfer";
  191. case -EINVAL:
  192. return "invalid argument";
  193. case -ECONNRESET:
  194. return "unlinked (async)";
  195. case -ESHUTDOWN:
  196. return "device shut down";
  197. default:
  198. snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
  199. return unkmsg;
  200. }
  201. }
  202. /* usb_pipetype_str
  203. * retrieve string representation of USB pipe type
  204. */
  205. static inline char *usb_pipetype_str(int pipe)
  206. {
  207. if (usb_pipeisoc(pipe))
  208. return "Isoc";
  209. if (usb_pipeint(pipe))
  210. return "Int";
  211. if (usb_pipecontrol(pipe))
  212. return "Ctrl";
  213. if (usb_pipebulk(pipe))
  214. return "Bulk";
  215. return "?";
  216. }
  217. /* dump_urb
  218. * write content of URB to syslog for debugging
  219. */
  220. static inline void dump_urb(enum debuglevel level, const char *tag,
  221. struct urb *urb)
  222. {
  223. #ifdef CONFIG_GIGASET_DEBUG
  224. int i;
  225. gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
  226. if (urb) {
  227. gig_dbg(level,
  228. " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
  229. "hcpriv=0x%08lx, transfer_flags=0x%x,",
  230. (unsigned long) urb->dev,
  231. usb_pipetype_str(urb->pipe),
  232. usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
  233. usb_pipein(urb->pipe) ? "in" : "out",
  234. (unsigned long) urb->hcpriv,
  235. urb->transfer_flags);
  236. gig_dbg(level,
  237. " transfer_buffer=0x%08lx[%d], actual_length=%d, "
  238. "setup_packet=0x%08lx,",
  239. (unsigned long) urb->transfer_buffer,
  240. urb->transfer_buffer_length, urb->actual_length,
  241. (unsigned long) urb->setup_packet);
  242. gig_dbg(level,
  243. " start_frame=%d, number_of_packets=%d, interval=%d, "
  244. "error_count=%d,",
  245. urb->start_frame, urb->number_of_packets, urb->interval,
  246. urb->error_count);
  247. gig_dbg(level,
  248. " context=0x%08lx, complete=0x%08lx, "
  249. "iso_frame_desc[]={",
  250. (unsigned long) urb->context,
  251. (unsigned long) urb->complete);
  252. for (i = 0; i < urb->number_of_packets; i++) {
  253. struct usb_iso_packet_descriptor *pifd
  254. = &urb->iso_frame_desc[i];
  255. gig_dbg(level,
  256. " {offset=%u, length=%u, actual_length=%u, "
  257. "status=%u}",
  258. pifd->offset, pifd->length, pifd->actual_length,
  259. pifd->status);
  260. }
  261. }
  262. gig_dbg(level, "}}");
  263. #endif
  264. }
  265. /* read/set modem control bits etc. (m10x only) */
  266. static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
  267. unsigned new_state)
  268. {
  269. return -EINVAL;
  270. }
  271. static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
  272. {
  273. return -EINVAL;
  274. }
  275. static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
  276. {
  277. return -EINVAL;
  278. }
  279. /* set/clear bits in base connection state, return previous state
  280. */
  281. static inline int update_basstate(struct bas_cardstate *ucs,
  282. int set, int clear)
  283. {
  284. unsigned long flags;
  285. int state;
  286. spin_lock_irqsave(&ucs->lock, flags);
  287. state = ucs->basstate;
  288. ucs->basstate = (state & ~clear) | set;
  289. spin_unlock_irqrestore(&ucs->lock, flags);
  290. return state;
  291. }
  292. /* error_hangup
  293. * hang up any existing connection because of an unrecoverable error
  294. * This function may be called from any context and takes care of scheduling
  295. * the necessary actions for execution outside of interrupt context.
  296. * cs->lock must not be held.
  297. * argument:
  298. * B channel control structure
  299. */
  300. static inline void error_hangup(struct bc_state *bcs)
  301. {
  302. struct cardstate *cs = bcs->cs;
  303. gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
  304. gigaset_schedule_event(cs);
  305. }
  306. /* error_reset
  307. * reset Gigaset device because of an unrecoverable error
  308. * This function may be called from any context, and takes care of
  309. * scheduling the necessary actions for execution outside of interrupt context.
  310. * cs->lock must not be held.
  311. * argument:
  312. * controller state structure
  313. */
  314. static inline void error_reset(struct cardstate *cs)
  315. {
  316. /* reset interrupt pipe to recover (ignore errors) */
  317. update_basstate(cs->hw.bas, BS_RESETTING, 0);
  318. req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
  319. }
  320. /* check_pending
  321. * check for completion of pending control request
  322. * parameter:
  323. * ucs hardware specific controller state structure
  324. */
  325. static void check_pending(struct bas_cardstate *ucs)
  326. {
  327. unsigned long flags;
  328. spin_lock_irqsave(&ucs->lock, flags);
  329. switch (ucs->pending) {
  330. case 0:
  331. break;
  332. case HD_OPEN_ATCHANNEL:
  333. if (ucs->basstate & BS_ATOPEN)
  334. ucs->pending = 0;
  335. break;
  336. case HD_OPEN_B1CHANNEL:
  337. if (ucs->basstate & BS_B1OPEN)
  338. ucs->pending = 0;
  339. break;
  340. case HD_OPEN_B2CHANNEL:
  341. if (ucs->basstate & BS_B2OPEN)
  342. ucs->pending = 0;
  343. break;
  344. case HD_CLOSE_ATCHANNEL:
  345. if (!(ucs->basstate & BS_ATOPEN))
  346. ucs->pending = 0;
  347. break;
  348. case HD_CLOSE_B1CHANNEL:
  349. if (!(ucs->basstate & BS_B1OPEN))
  350. ucs->pending = 0;
  351. break;
  352. case HD_CLOSE_B2CHANNEL:
  353. if (!(ucs->basstate & BS_B2OPEN))
  354. ucs->pending = 0;
  355. break;
  356. case HD_DEVICE_INIT_ACK: /* no reply expected */
  357. ucs->pending = 0;
  358. break;
  359. case HD_RESET_INTERRUPT_PIPE:
  360. if (!(ucs->basstate & BS_RESETTING))
  361. ucs->pending = 0;
  362. break;
  363. /*
  364. * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
  365. * and should never end up here
  366. */
  367. default:
  368. dev_warn(&ucs->interface->dev,
  369. "unknown pending request 0x%02x cleared\n",
  370. ucs->pending);
  371. ucs->pending = 0;
  372. }
  373. if (!ucs->pending)
  374. del_timer(&ucs->timer_ctrl);
  375. spin_unlock_irqrestore(&ucs->lock, flags);
  376. }
  377. /* cmd_in_timeout
  378. * timeout routine for command input request
  379. * argument:
  380. * controller state structure
  381. */
  382. static void cmd_in_timeout(unsigned long data)
  383. {
  384. struct cardstate *cs = (struct cardstate *) data;
  385. struct bas_cardstate *ucs = cs->hw.bas;
  386. int rc;
  387. if (!ucs->rcvbuf_size) {
  388. gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
  389. return;
  390. }
  391. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  392. dev_notice(cs->dev, "control read: timeout, retry %d\n",
  393. ucs->retry_cmd_in);
  394. rc = atread_submit(cs, BAS_TIMEOUT);
  395. if (rc >= 0 || rc == -ENODEV)
  396. /* resubmitted or disconnected */
  397. /* - bypass regular exit block */
  398. return;
  399. } else {
  400. dev_err(cs->dev,
  401. "control read: timeout, giving up after %d tries\n",
  402. ucs->retry_cmd_in);
  403. }
  404. kfree(ucs->rcvbuf);
  405. ucs->rcvbuf = NULL;
  406. ucs->rcvbuf_size = 0;
  407. error_reset(cs);
  408. }
  409. /* read_ctrl_callback
  410. * USB completion handler for control pipe input
  411. * called by the USB subsystem in interrupt context
  412. * parameter:
  413. * urb USB request block
  414. * urb->context = inbuf structure for controller state
  415. */
  416. static void read_ctrl_callback(struct urb *urb)
  417. {
  418. struct inbuf_t *inbuf = urb->context;
  419. struct cardstate *cs = inbuf->cs;
  420. struct bas_cardstate *ucs = cs->hw.bas;
  421. int status = urb->status;
  422. int have_data = 0;
  423. unsigned numbytes;
  424. int rc;
  425. update_basstate(ucs, 0, BS_ATRDPEND);
  426. wake_up(&ucs->waitqueue);
  427. if (!ucs->rcvbuf_size) {
  428. dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
  429. return;
  430. }
  431. del_timer(&ucs->timer_cmd_in);
  432. switch (status) {
  433. case 0: /* normal completion */
  434. numbytes = urb->actual_length;
  435. if (unlikely(numbytes != ucs->rcvbuf_size)) {
  436. dev_warn(cs->dev,
  437. "control read: received %d chars, expected %d\n",
  438. numbytes, ucs->rcvbuf_size);
  439. if (numbytes > ucs->rcvbuf_size)
  440. numbytes = ucs->rcvbuf_size;
  441. }
  442. /* copy received bytes to inbuf */
  443. have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
  444. if (unlikely(numbytes < ucs->rcvbuf_size)) {
  445. /* incomplete - resubmit for remaining bytes */
  446. ucs->rcvbuf_size -= numbytes;
  447. ucs->retry_cmd_in = 0;
  448. rc = atread_submit(cs, BAS_TIMEOUT);
  449. if (rc >= 0 || rc == -ENODEV)
  450. /* resubmitted or disconnected */
  451. /* - bypass regular exit block */
  452. return;
  453. error_reset(cs);
  454. }
  455. break;
  456. case -ENOENT: /* cancelled */
  457. case -ECONNRESET: /* cancelled (async) */
  458. case -EINPROGRESS: /* pending */
  459. case -ENODEV: /* device removed */
  460. case -ESHUTDOWN: /* device shut down */
  461. /* no action necessary */
  462. gig_dbg(DEBUG_USBREQ, "%s: %s",
  463. __func__, get_usb_statmsg(status));
  464. break;
  465. default: /* severe trouble */
  466. dev_warn(cs->dev, "control read: %s\n",
  467. get_usb_statmsg(status));
  468. if (ucs->retry_cmd_in++ < BAS_RETRY) {
  469. dev_notice(cs->dev, "control read: retry %d\n",
  470. ucs->retry_cmd_in);
  471. rc = atread_submit(cs, BAS_TIMEOUT);
  472. if (rc >= 0 || rc == -ENODEV)
  473. /* resubmitted or disconnected */
  474. /* - bypass regular exit block */
  475. return;
  476. } else {
  477. dev_err(cs->dev,
  478. "control read: giving up after %d tries\n",
  479. ucs->retry_cmd_in);
  480. }
  481. error_reset(cs);
  482. }
  483. kfree(ucs->rcvbuf);
  484. ucs->rcvbuf = NULL;
  485. ucs->rcvbuf_size = 0;
  486. if (have_data) {
  487. gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
  488. gigaset_schedule_event(cs);
  489. }
  490. }
  491. /* atread_submit
  492. * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
  493. * parameters:
  494. * cs controller state structure
  495. * timeout timeout in 1/10 sec., 0: none
  496. * return value:
  497. * 0 on success
  498. * -EBUSY if another request is pending
  499. * any URB submission error code
  500. */
  501. static int atread_submit(struct cardstate *cs, int timeout)
  502. {
  503. struct bas_cardstate *ucs = cs->hw.bas;
  504. int basstate;
  505. int ret;
  506. gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
  507. ucs->rcvbuf_size);
  508. basstate = update_basstate(ucs, BS_ATRDPEND, 0);
  509. if (basstate & BS_ATRDPEND) {
  510. dev_err(cs->dev,
  511. "could not submit HD_READ_ATMESSAGE: URB busy\n");
  512. return -EBUSY;
  513. }
  514. if (basstate & BS_SUSPEND) {
  515. dev_notice(cs->dev,
  516. "HD_READ_ATMESSAGE not submitted, "
  517. "suspend in progress\n");
  518. update_basstate(ucs, 0, BS_ATRDPEND);
  519. /* treat like disconnect */
  520. return -ENODEV;
  521. }
  522. ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
  523. ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
  524. ucs->dr_cmd_in.wValue = 0;
  525. ucs->dr_cmd_in.wIndex = 0;
  526. ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
  527. usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
  528. usb_rcvctrlpipe(ucs->udev, 0),
  529. (unsigned char *) &ucs->dr_cmd_in,
  530. ucs->rcvbuf, ucs->rcvbuf_size,
  531. read_ctrl_callback, cs->inbuf);
  532. ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
  533. if (ret != 0) {
  534. update_basstate(ucs, 0, BS_ATRDPEND);
  535. dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
  536. get_usb_rcmsg(ret));
  537. return ret;
  538. }
  539. if (timeout > 0) {
  540. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  541. ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
  542. ucs->timer_cmd_in.data = (unsigned long) cs;
  543. ucs->timer_cmd_in.function = cmd_in_timeout;
  544. add_timer(&ucs->timer_cmd_in);
  545. }
  546. return 0;
  547. }
  548. /* read_int_callback
  549. * USB completion handler for interrupt pipe input
  550. * called by the USB subsystem in interrupt context
  551. * parameter:
  552. * urb USB request block
  553. * urb->context = controller state structure
  554. */
  555. static void read_int_callback(struct urb *urb)
  556. {
  557. struct cardstate *cs = urb->context;
  558. struct bas_cardstate *ucs = cs->hw.bas;
  559. struct bc_state *bcs;
  560. int status = urb->status;
  561. unsigned long flags;
  562. int rc;
  563. unsigned l;
  564. int channel;
  565. switch (status) {
  566. case 0: /* success */
  567. break;
  568. case -ENOENT: /* cancelled */
  569. case -ECONNRESET: /* cancelled (async) */
  570. case -EINPROGRESS: /* pending */
  571. /* ignore silently */
  572. gig_dbg(DEBUG_USBREQ, "%s: %s",
  573. __func__, get_usb_statmsg(status));
  574. return;
  575. case -ENODEV: /* device removed */
  576. case -ESHUTDOWN: /* device shut down */
  577. gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
  578. return;
  579. default: /* severe trouble */
  580. dev_warn(cs->dev, "interrupt read: %s\n",
  581. get_usb_statmsg(status));
  582. goto resubmit;
  583. }
  584. /* drop incomplete packets even if the missing bytes wouldn't matter */
  585. if (unlikely(urb->actual_length < IP_MSGSIZE)) {
  586. dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
  587. urb->actual_length);
  588. goto resubmit;
  589. }
  590. l = (unsigned) ucs->int_in_buf[1] +
  591. (((unsigned) ucs->int_in_buf[2]) << 8);
  592. gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
  593. urb->actual_length, (int)ucs->int_in_buf[0], l,
  594. (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
  595. channel = 0;
  596. switch (ucs->int_in_buf[0]) {
  597. case HD_DEVICE_INIT_OK:
  598. update_basstate(ucs, BS_INIT, 0);
  599. break;
  600. case HD_READY_SEND_ATDATA:
  601. del_timer(&ucs->timer_atrdy);
  602. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  603. start_cbsend(cs);
  604. break;
  605. case HD_OPEN_B2CHANNEL_ACK:
  606. ++channel;
  607. case HD_OPEN_B1CHANNEL_ACK:
  608. bcs = cs->bcs + channel;
  609. update_basstate(ucs, BS_B1OPEN << channel, 0);
  610. gigaset_bchannel_up(bcs);
  611. break;
  612. case HD_OPEN_ATCHANNEL_ACK:
  613. update_basstate(ucs, BS_ATOPEN, 0);
  614. start_cbsend(cs);
  615. break;
  616. case HD_CLOSE_B2CHANNEL_ACK:
  617. ++channel;
  618. case HD_CLOSE_B1CHANNEL_ACK:
  619. bcs = cs->bcs + channel;
  620. update_basstate(ucs, 0, BS_B1OPEN << channel);
  621. stopurbs(bcs->hw.bas);
  622. gigaset_bchannel_down(bcs);
  623. break;
  624. case HD_CLOSE_ATCHANNEL_ACK:
  625. update_basstate(ucs, 0, BS_ATOPEN);
  626. break;
  627. case HD_B2_FLOW_CONTROL:
  628. ++channel;
  629. case HD_B1_FLOW_CONTROL:
  630. bcs = cs->bcs + channel;
  631. atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
  632. &bcs->hw.bas->corrbytes);
  633. gig_dbg(DEBUG_ISO,
  634. "Flow control (channel %d, sub %d): 0x%02x => %d",
  635. channel, bcs->hw.bas->numsub, l,
  636. atomic_read(&bcs->hw.bas->corrbytes));
  637. break;
  638. case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
  639. if (!l) {
  640. dev_warn(cs->dev,
  641. "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
  642. break;
  643. }
  644. spin_lock_irqsave(&cs->lock, flags);
  645. if (ucs->rcvbuf_size) {
  646. /* throw away previous buffer - we have no queue */
  647. dev_err(cs->dev,
  648. "receive AT data overrun, %d bytes lost\n",
  649. ucs->rcvbuf_size);
  650. kfree(ucs->rcvbuf);
  651. ucs->rcvbuf_size = 0;
  652. }
  653. ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
  654. if (ucs->rcvbuf == NULL) {
  655. spin_unlock_irqrestore(&cs->lock, flags);
  656. dev_err(cs->dev, "out of memory receiving AT data\n");
  657. error_reset(cs);
  658. break;
  659. }
  660. ucs->rcvbuf_size = l;
  661. ucs->retry_cmd_in = 0;
  662. rc = atread_submit(cs, BAS_TIMEOUT);
  663. if (rc < 0) {
  664. kfree(ucs->rcvbuf);
  665. ucs->rcvbuf = NULL;
  666. ucs->rcvbuf_size = 0;
  667. if (rc != -ENODEV) {
  668. spin_unlock_irqrestore(&cs->lock, flags);
  669. error_reset(cs);
  670. break;
  671. }
  672. }
  673. spin_unlock_irqrestore(&cs->lock, flags);
  674. break;
  675. case HD_RESET_INTERRUPT_PIPE_ACK:
  676. update_basstate(ucs, 0, BS_RESETTING);
  677. dev_notice(cs->dev, "interrupt pipe reset\n");
  678. break;
  679. case HD_SUSPEND_END:
  680. gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
  681. break;
  682. default:
  683. dev_warn(cs->dev,
  684. "unknown Gigaset signal 0x%02x (%u) ignored\n",
  685. (int) ucs->int_in_buf[0], l);
  686. }
  687. check_pending(ucs);
  688. wake_up(&ucs->waitqueue);
  689. resubmit:
  690. rc = usb_submit_urb(urb, GFP_ATOMIC);
  691. if (unlikely(rc != 0 && rc != -ENODEV)) {
  692. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  693. get_usb_rcmsg(rc));
  694. error_reset(cs);
  695. }
  696. }
  697. /* read_iso_callback
  698. * USB completion handler for B channel isochronous input
  699. * called by the USB subsystem in interrupt context
  700. * parameter:
  701. * urb USB request block of completed request
  702. * urb->context = bc_state structure
  703. */
  704. static void read_iso_callback(struct urb *urb)
  705. {
  706. struct bc_state *bcs;
  707. struct bas_bc_state *ubc;
  708. int status = urb->status;
  709. unsigned long flags;
  710. int i, rc;
  711. /* status codes not worth bothering the tasklet with */
  712. if (unlikely(status == -ENOENT ||
  713. status == -ECONNRESET ||
  714. status == -EINPROGRESS ||
  715. status == -ENODEV ||
  716. status == -ESHUTDOWN)) {
  717. gig_dbg(DEBUG_ISO, "%s: %s",
  718. __func__, get_usb_statmsg(status));
  719. return;
  720. }
  721. bcs = urb->context;
  722. ubc = bcs->hw.bas;
  723. spin_lock_irqsave(&ubc->isoinlock, flags);
  724. if (likely(ubc->isoindone == NULL)) {
  725. /* pass URB to tasklet */
  726. ubc->isoindone = urb;
  727. ubc->isoinstatus = status;
  728. tasklet_hi_schedule(&ubc->rcvd_tasklet);
  729. } else {
  730. /* tasklet still busy, drop data and resubmit URB */
  731. ubc->loststatus = status;
  732. for (i = 0; i < BAS_NUMFRAMES; i++) {
  733. ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
  734. if (unlikely(urb->iso_frame_desc[i].status != 0 &&
  735. urb->iso_frame_desc[i].status !=
  736. -EINPROGRESS))
  737. ubc->loststatus = urb->iso_frame_desc[i].status;
  738. urb->iso_frame_desc[i].status = 0;
  739. urb->iso_frame_desc[i].actual_length = 0;
  740. }
  741. if (likely(ubc->running)) {
  742. /* urb->dev is clobbered by USB subsystem */
  743. urb->dev = bcs->cs->hw.bas->udev;
  744. urb->transfer_flags = URB_ISO_ASAP;
  745. urb->number_of_packets = BAS_NUMFRAMES;
  746. gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
  747. __func__);
  748. rc = usb_submit_urb(urb, GFP_ATOMIC);
  749. if (unlikely(rc != 0 && rc != -ENODEV)) {
  750. dev_err(bcs->cs->dev,
  751. "could not resubmit isochronous read "
  752. "URB: %s\n", get_usb_rcmsg(rc));
  753. dump_urb(DEBUG_ISO, "isoc read", urb);
  754. error_hangup(bcs);
  755. }
  756. }
  757. }
  758. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  759. }
  760. /* write_iso_callback
  761. * USB completion handler for B channel isochronous output
  762. * called by the USB subsystem in interrupt context
  763. * parameter:
  764. * urb USB request block of completed request
  765. * urb->context = isow_urbctx_t structure
  766. */
  767. static void write_iso_callback(struct urb *urb)
  768. {
  769. struct isow_urbctx_t *ucx;
  770. struct bas_bc_state *ubc;
  771. int status = urb->status;
  772. unsigned long flags;
  773. /* status codes not worth bothering the tasklet with */
  774. if (unlikely(status == -ENOENT ||
  775. status == -ECONNRESET ||
  776. status == -EINPROGRESS ||
  777. status == -ENODEV ||
  778. status == -ESHUTDOWN)) {
  779. gig_dbg(DEBUG_ISO, "%s: %s",
  780. __func__, get_usb_statmsg(status));
  781. return;
  782. }
  783. /* pass URB context to tasklet */
  784. ucx = urb->context;
  785. ubc = ucx->bcs->hw.bas;
  786. ucx->status = status;
  787. spin_lock_irqsave(&ubc->isooutlock, flags);
  788. ubc->isooutovfl = ubc->isooutdone;
  789. ubc->isooutdone = ucx;
  790. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  791. tasklet_hi_schedule(&ubc->sent_tasklet);
  792. }
  793. /* starturbs
  794. * prepare and submit USB request blocks for isochronous input and output
  795. * argument:
  796. * B channel control structure
  797. * return value:
  798. * 0 on success
  799. * < 0 on error (no URBs submitted)
  800. */
  801. static int starturbs(struct bc_state *bcs)
  802. {
  803. struct bas_bc_state *ubc = bcs->hw.bas;
  804. struct urb *urb;
  805. int j, k;
  806. int rc;
  807. /* initialize L2 reception */
  808. if (bcs->proto2 == L2_HDLC)
  809. bcs->inputstate |= INS_flag_hunt;
  810. /* submit all isochronous input URBs */
  811. ubc->running = 1;
  812. for (k = 0; k < BAS_INURBS; k++) {
  813. urb = ubc->isoinurbs[k];
  814. if (!urb) {
  815. rc = -EFAULT;
  816. goto error;
  817. }
  818. urb->dev = bcs->cs->hw.bas->udev;
  819. urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
  820. urb->transfer_flags = URB_ISO_ASAP;
  821. urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
  822. urb->transfer_buffer_length = BAS_INBUFSIZE;
  823. urb->number_of_packets = BAS_NUMFRAMES;
  824. urb->interval = BAS_FRAMETIME;
  825. urb->complete = read_iso_callback;
  826. urb->context = bcs;
  827. for (j = 0; j < BAS_NUMFRAMES; j++) {
  828. urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
  829. urb->iso_frame_desc[j].length = BAS_MAXFRAME;
  830. urb->iso_frame_desc[j].status = 0;
  831. urb->iso_frame_desc[j].actual_length = 0;
  832. }
  833. dump_urb(DEBUG_ISO, "Initial isoc read", urb);
  834. rc = usb_submit_urb(urb, GFP_ATOMIC);
  835. if (rc != 0)
  836. goto error;
  837. }
  838. /* initialize L2 transmission */
  839. gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
  840. /* set up isochronous output URBs for flag idling */
  841. for (k = 0; k < BAS_OUTURBS; ++k) {
  842. urb = ubc->isoouturbs[k].urb;
  843. if (!urb) {
  844. rc = -EFAULT;
  845. goto error;
  846. }
  847. urb->dev = bcs->cs->hw.bas->udev;
  848. urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
  849. urb->transfer_flags = URB_ISO_ASAP;
  850. urb->transfer_buffer = ubc->isooutbuf->data;
  851. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  852. urb->number_of_packets = BAS_NUMFRAMES;
  853. urb->interval = BAS_FRAMETIME;
  854. urb->complete = write_iso_callback;
  855. urb->context = &ubc->isoouturbs[k];
  856. for (j = 0; j < BAS_NUMFRAMES; ++j) {
  857. urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
  858. urb->iso_frame_desc[j].length = BAS_NORMFRAME;
  859. urb->iso_frame_desc[j].status = 0;
  860. urb->iso_frame_desc[j].actual_length = 0;
  861. }
  862. ubc->isoouturbs[k].limit = -1;
  863. }
  864. /* keep one URB free, submit the others */
  865. for (k = 0; k < BAS_OUTURBS-1; ++k) {
  866. dump_urb(DEBUG_ISO, "Initial isoc write", urb);
  867. rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
  868. if (rc != 0)
  869. goto error;
  870. }
  871. dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
  872. ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
  873. ubc->isooutdone = ubc->isooutovfl = NULL;
  874. return 0;
  875. error:
  876. stopurbs(ubc);
  877. return rc;
  878. }
  879. /* stopurbs
  880. * cancel the USB request blocks for isochronous input and output
  881. * errors are silently ignored
  882. * argument:
  883. * B channel control structure
  884. */
  885. static void stopurbs(struct bas_bc_state *ubc)
  886. {
  887. int k, rc;
  888. ubc->running = 0;
  889. for (k = 0; k < BAS_INURBS; ++k) {
  890. rc = usb_unlink_urb(ubc->isoinurbs[k]);
  891. gig_dbg(DEBUG_ISO,
  892. "%s: isoc input URB %d unlinked, result = %s",
  893. __func__, k, get_usb_rcmsg(rc));
  894. }
  895. for (k = 0; k < BAS_OUTURBS; ++k) {
  896. rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
  897. gig_dbg(DEBUG_ISO,
  898. "%s: isoc output URB %d unlinked, result = %s",
  899. __func__, k, get_usb_rcmsg(rc));
  900. }
  901. }
  902. /* Isochronous Write - Bottom Half */
  903. /* =============================== */
  904. /* submit_iso_write_urb
  905. * fill and submit the next isochronous write URB
  906. * parameters:
  907. * ucx context structure containing URB
  908. * return value:
  909. * number of frames submitted in URB
  910. * 0 if URB not submitted because no data available (isooutbuf busy)
  911. * error code < 0 on error
  912. */
  913. static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
  914. {
  915. struct urb *urb = ucx->urb;
  916. struct bas_bc_state *ubc = ucx->bcs->hw.bas;
  917. struct usb_iso_packet_descriptor *ifd;
  918. int corrbytes, nframe, rc;
  919. /* urb->dev is clobbered by USB subsystem */
  920. urb->dev = ucx->bcs->cs->hw.bas->udev;
  921. urb->transfer_flags = URB_ISO_ASAP;
  922. urb->transfer_buffer = ubc->isooutbuf->data;
  923. urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
  924. for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
  925. ifd = &urb->iso_frame_desc[nframe];
  926. /* compute frame length according to flow control */
  927. ifd->length = BAS_NORMFRAME;
  928. corrbytes = atomic_read(&ubc->corrbytes);
  929. if (corrbytes != 0) {
  930. gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
  931. __func__, corrbytes);
  932. if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
  933. corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
  934. else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
  935. corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
  936. ifd->length += corrbytes;
  937. atomic_add(-corrbytes, &ubc->corrbytes);
  938. }
  939. /* retrieve block of data to send */
  940. rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
  941. if (rc < 0) {
  942. if (rc == -EBUSY) {
  943. gig_dbg(DEBUG_ISO,
  944. "%s: buffer busy at frame %d",
  945. __func__, nframe);
  946. /* tasklet will be restarted from
  947. gigaset_isoc_send_skb() */
  948. } else {
  949. dev_err(ucx->bcs->cs->dev,
  950. "%s: buffer error %d at frame %d\n",
  951. __func__, rc, nframe);
  952. return rc;
  953. }
  954. break;
  955. }
  956. ifd->offset = rc;
  957. ucx->limit = ubc->isooutbuf->nextread;
  958. ifd->status = 0;
  959. ifd->actual_length = 0;
  960. }
  961. if (unlikely(nframe == 0))
  962. return 0; /* no data to send */
  963. urb->number_of_packets = nframe;
  964. rc = usb_submit_urb(urb, GFP_ATOMIC);
  965. if (unlikely(rc)) {
  966. if (rc == -ENODEV)
  967. /* device removed - give up silently */
  968. gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
  969. else
  970. dev_err(ucx->bcs->cs->dev,
  971. "could not submit isochronous write URB: %s\n",
  972. get_usb_rcmsg(rc));
  973. return rc;
  974. }
  975. ++ubc->numsub;
  976. return nframe;
  977. }
  978. /* write_iso_tasklet
  979. * tasklet scheduled when an isochronous output URB from the Gigaset device
  980. * has completed
  981. * parameter:
  982. * data B channel state structure
  983. */
  984. static void write_iso_tasklet(unsigned long data)
  985. {
  986. struct bc_state *bcs = (struct bc_state *) data;
  987. struct bas_bc_state *ubc = bcs->hw.bas;
  988. struct cardstate *cs = bcs->cs;
  989. struct isow_urbctx_t *done, *next, *ovfl;
  990. struct urb *urb;
  991. int status;
  992. struct usb_iso_packet_descriptor *ifd;
  993. int offset;
  994. unsigned long flags;
  995. int i;
  996. struct sk_buff *skb;
  997. int len;
  998. int rc;
  999. /* loop while completed URBs arrive in time */
  1000. for (;;) {
  1001. if (unlikely(!(ubc->running))) {
  1002. gig_dbg(DEBUG_ISO, "%s: not running", __func__);
  1003. return;
  1004. }
  1005. /* retrieve completed URBs */
  1006. spin_lock_irqsave(&ubc->isooutlock, flags);
  1007. done = ubc->isooutdone;
  1008. ubc->isooutdone = NULL;
  1009. ovfl = ubc->isooutovfl;
  1010. ubc->isooutovfl = NULL;
  1011. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1012. if (ovfl) {
  1013. dev_err(cs->dev, "isochronous write buffer underrun\n");
  1014. error_hangup(bcs);
  1015. break;
  1016. }
  1017. if (!done)
  1018. break;
  1019. /* submit free URB if available */
  1020. spin_lock_irqsave(&ubc->isooutlock, flags);
  1021. next = ubc->isooutfree;
  1022. ubc->isooutfree = NULL;
  1023. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1024. if (next) {
  1025. rc = submit_iso_write_urb(next);
  1026. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1027. /* could not submit URB, put it back */
  1028. spin_lock_irqsave(&ubc->isooutlock, flags);
  1029. if (ubc->isooutfree == NULL) {
  1030. ubc->isooutfree = next;
  1031. next = NULL;
  1032. }
  1033. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1034. if (next) {
  1035. /* couldn't put it back */
  1036. dev_err(cs->dev,
  1037. "losing isochronous write URB\n");
  1038. error_hangup(bcs);
  1039. }
  1040. }
  1041. }
  1042. /* process completed URB */
  1043. urb = done->urb;
  1044. status = done->status;
  1045. switch (status) {
  1046. case -EXDEV: /* partial completion */
  1047. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1048. __func__);
  1049. /* fall through - what's the difference anyway? */
  1050. case 0: /* normal completion */
  1051. /* inspect individual frames
  1052. * assumptions (for lack of documentation):
  1053. * - actual_length bytes of first frame in error are
  1054. * successfully sent
  1055. * - all following frames are not sent at all
  1056. */
  1057. offset = done->limit; /* default (no error) */
  1058. for (i = 0; i < BAS_NUMFRAMES; i++) {
  1059. ifd = &urb->iso_frame_desc[i];
  1060. if (ifd->status ||
  1061. ifd->actual_length != ifd->length) {
  1062. dev_warn(cs->dev,
  1063. "isochronous write: frame %d: %s, "
  1064. "only %d of %d bytes sent\n",
  1065. i, get_usb_statmsg(ifd->status),
  1066. ifd->actual_length, ifd->length);
  1067. offset = (ifd->offset +
  1068. ifd->actual_length)
  1069. % BAS_OUTBUFSIZE;
  1070. break;
  1071. }
  1072. }
  1073. break;
  1074. case -EPIPE: /* stall - probably underrun */
  1075. dev_err(cs->dev, "isochronous write stalled\n");
  1076. error_hangup(bcs);
  1077. break;
  1078. default: /* severe trouble */
  1079. dev_warn(cs->dev, "isochronous write: %s\n",
  1080. get_usb_statmsg(status));
  1081. }
  1082. /* mark the write buffer area covered by this URB as free */
  1083. if (done->limit >= 0)
  1084. ubc->isooutbuf->read = done->limit;
  1085. /* mark URB as free */
  1086. spin_lock_irqsave(&ubc->isooutlock, flags);
  1087. next = ubc->isooutfree;
  1088. ubc->isooutfree = done;
  1089. spin_unlock_irqrestore(&ubc->isooutlock, flags);
  1090. if (next) {
  1091. /* only one URB still active - resubmit one */
  1092. rc = submit_iso_write_urb(next);
  1093. if (unlikely(rc <= 0 && rc != -ENODEV)) {
  1094. /* couldn't submit */
  1095. error_hangup(bcs);
  1096. }
  1097. }
  1098. }
  1099. /* process queued SKBs */
  1100. while ((skb = skb_dequeue(&bcs->squeue))) {
  1101. /* copy to output buffer, doing L2 encapsulation */
  1102. len = skb->len;
  1103. if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
  1104. /* insufficient buffer space, push back onto queue */
  1105. skb_queue_head(&bcs->squeue, skb);
  1106. gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
  1107. __func__, skb_queue_len(&bcs->squeue));
  1108. break;
  1109. }
  1110. skb_pull(skb, len);
  1111. gigaset_skb_sent(bcs, skb);
  1112. dev_kfree_skb_any(skb);
  1113. }
  1114. }
  1115. /* Isochronous Read - Bottom Half */
  1116. /* ============================== */
  1117. /* read_iso_tasklet
  1118. * tasklet scheduled when an isochronous input URB from the Gigaset device
  1119. * has completed
  1120. * parameter:
  1121. * data B channel state structure
  1122. */
  1123. static void read_iso_tasklet(unsigned long data)
  1124. {
  1125. struct bc_state *bcs = (struct bc_state *) data;
  1126. struct bas_bc_state *ubc = bcs->hw.bas;
  1127. struct cardstate *cs = bcs->cs;
  1128. struct urb *urb;
  1129. int status;
  1130. char *rcvbuf;
  1131. unsigned long flags;
  1132. int totleft, numbytes, offset, frame, rc;
  1133. /* loop while more completed URBs arrive in the meantime */
  1134. for (;;) {
  1135. /* retrieve URB */
  1136. spin_lock_irqsave(&ubc->isoinlock, flags);
  1137. urb = ubc->isoindone;
  1138. if (!urb) {
  1139. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1140. return;
  1141. }
  1142. status = ubc->isoinstatus;
  1143. ubc->isoindone = NULL;
  1144. if (unlikely(ubc->loststatus != -EINPROGRESS)) {
  1145. dev_warn(cs->dev,
  1146. "isochronous read overrun, "
  1147. "dropped URB with status: %s, %d bytes lost\n",
  1148. get_usb_statmsg(ubc->loststatus),
  1149. ubc->isoinlost);
  1150. ubc->loststatus = -EINPROGRESS;
  1151. }
  1152. spin_unlock_irqrestore(&ubc->isoinlock, flags);
  1153. if (unlikely(!(ubc->running))) {
  1154. gig_dbg(DEBUG_ISO,
  1155. "%s: channel not running, "
  1156. "dropped URB with status: %s",
  1157. __func__, get_usb_statmsg(status));
  1158. return;
  1159. }
  1160. switch (status) {
  1161. case 0: /* normal completion */
  1162. break;
  1163. case -EXDEV: /* inspect individual frames
  1164. (we do that anyway) */
  1165. gig_dbg(DEBUG_ISO, "%s: URB partially completed",
  1166. __func__);
  1167. break;
  1168. case -ENOENT:
  1169. case -ECONNRESET:
  1170. case -EINPROGRESS:
  1171. gig_dbg(DEBUG_ISO, "%s: %s",
  1172. __func__, get_usb_statmsg(status));
  1173. continue; /* -> skip */
  1174. case -EPIPE:
  1175. dev_err(cs->dev, "isochronous read stalled\n");
  1176. error_hangup(bcs);
  1177. continue; /* -> skip */
  1178. default: /* severe trouble */
  1179. dev_warn(cs->dev, "isochronous read: %s\n",
  1180. get_usb_statmsg(status));
  1181. goto error;
  1182. }
  1183. rcvbuf = urb->transfer_buffer;
  1184. totleft = urb->actual_length;
  1185. for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
  1186. numbytes = urb->iso_frame_desc[frame].actual_length;
  1187. if (unlikely(urb->iso_frame_desc[frame].status))
  1188. dev_warn(cs->dev,
  1189. "isochronous read: frame %d[%d]: %s\n",
  1190. frame, numbytes,
  1191. get_usb_statmsg(
  1192. urb->iso_frame_desc[frame].status));
  1193. if (unlikely(numbytes > BAS_MAXFRAME))
  1194. dev_warn(cs->dev,
  1195. "isochronous read: frame %d: "
  1196. "numbytes (%d) > BAS_MAXFRAME\n",
  1197. frame, numbytes);
  1198. if (unlikely(numbytes > totleft)) {
  1199. dev_warn(cs->dev,
  1200. "isochronous read: frame %d: "
  1201. "numbytes (%d) > totleft (%d)\n",
  1202. frame, numbytes, totleft);
  1203. numbytes = totleft;
  1204. }
  1205. offset = urb->iso_frame_desc[frame].offset;
  1206. if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
  1207. dev_warn(cs->dev,
  1208. "isochronous read: frame %d: "
  1209. "offset (%d) + numbytes (%d) "
  1210. "> BAS_INBUFSIZE\n",
  1211. frame, offset, numbytes);
  1212. numbytes = BAS_INBUFSIZE - offset;
  1213. }
  1214. gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
  1215. totleft -= numbytes;
  1216. }
  1217. if (unlikely(totleft > 0))
  1218. dev_warn(cs->dev,
  1219. "isochronous read: %d data bytes missing\n",
  1220. totleft);
  1221. error:
  1222. /* URB processed, resubmit */
  1223. for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
  1224. urb->iso_frame_desc[frame].status = 0;
  1225. urb->iso_frame_desc[frame].actual_length = 0;
  1226. }
  1227. /* urb->dev is clobbered by USB subsystem */
  1228. urb->dev = bcs->cs->hw.bas->udev;
  1229. urb->transfer_flags = URB_ISO_ASAP;
  1230. urb->number_of_packets = BAS_NUMFRAMES;
  1231. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1232. if (unlikely(rc != 0 && rc != -ENODEV)) {
  1233. dev_err(cs->dev,
  1234. "could not resubmit isochronous read URB: %s\n",
  1235. get_usb_rcmsg(rc));
  1236. dump_urb(DEBUG_ISO, "resubmit iso read", urb);
  1237. error_hangup(bcs);
  1238. }
  1239. }
  1240. }
  1241. /* Channel Operations */
  1242. /* ================== */
  1243. /* req_timeout
  1244. * timeout routine for control output request
  1245. * argument:
  1246. * B channel control structure
  1247. */
  1248. static void req_timeout(unsigned long data)
  1249. {
  1250. struct bc_state *bcs = (struct bc_state *) data;
  1251. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1252. int pending;
  1253. unsigned long flags;
  1254. check_pending(ucs);
  1255. spin_lock_irqsave(&ucs->lock, flags);
  1256. pending = ucs->pending;
  1257. ucs->pending = 0;
  1258. spin_unlock_irqrestore(&ucs->lock, flags);
  1259. switch (pending) {
  1260. case 0: /* no pending request */
  1261. gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
  1262. break;
  1263. case HD_OPEN_ATCHANNEL:
  1264. dev_err(bcs->cs->dev, "timeout opening AT channel\n");
  1265. error_reset(bcs->cs);
  1266. break;
  1267. case HD_OPEN_B2CHANNEL:
  1268. case HD_OPEN_B1CHANNEL:
  1269. dev_err(bcs->cs->dev, "timeout opening channel %d\n",
  1270. bcs->channel + 1);
  1271. error_hangup(bcs);
  1272. break;
  1273. case HD_CLOSE_ATCHANNEL:
  1274. dev_err(bcs->cs->dev, "timeout closing AT channel\n");
  1275. error_reset(bcs->cs);
  1276. break;
  1277. case HD_CLOSE_B2CHANNEL:
  1278. case HD_CLOSE_B1CHANNEL:
  1279. dev_err(bcs->cs->dev, "timeout closing channel %d\n",
  1280. bcs->channel + 1);
  1281. error_reset(bcs->cs);
  1282. break;
  1283. case HD_RESET_INTERRUPT_PIPE:
  1284. /* error recovery escalation */
  1285. dev_err(bcs->cs->dev,
  1286. "reset interrupt pipe timeout, attempting USB reset\n");
  1287. usb_queue_reset_device(bcs->cs->hw.bas->interface);
  1288. break;
  1289. default:
  1290. dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
  1291. pending);
  1292. }
  1293. wake_up(&ucs->waitqueue);
  1294. }
  1295. /* write_ctrl_callback
  1296. * USB completion handler for control pipe output
  1297. * called by the USB subsystem in interrupt context
  1298. * parameter:
  1299. * urb USB request block of completed request
  1300. * urb->context = hardware specific controller state structure
  1301. */
  1302. static void write_ctrl_callback(struct urb *urb)
  1303. {
  1304. struct bas_cardstate *ucs = urb->context;
  1305. int status = urb->status;
  1306. int rc;
  1307. unsigned long flags;
  1308. /* check status */
  1309. switch (status) {
  1310. case 0: /* normal completion */
  1311. spin_lock_irqsave(&ucs->lock, flags);
  1312. switch (ucs->pending) {
  1313. case HD_DEVICE_INIT_ACK: /* no reply expected */
  1314. del_timer(&ucs->timer_ctrl);
  1315. ucs->pending = 0;
  1316. break;
  1317. }
  1318. spin_unlock_irqrestore(&ucs->lock, flags);
  1319. return;
  1320. case -ENOENT: /* cancelled */
  1321. case -ECONNRESET: /* cancelled (async) */
  1322. case -EINPROGRESS: /* pending */
  1323. case -ENODEV: /* device removed */
  1324. case -ESHUTDOWN: /* device shut down */
  1325. /* ignore silently */
  1326. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1327. __func__, get_usb_statmsg(status));
  1328. break;
  1329. default: /* any failure */
  1330. /* don't retry if suspend requested */
  1331. if (++ucs->retry_ctrl > BAS_RETRY ||
  1332. (ucs->basstate & BS_SUSPEND)) {
  1333. dev_err(&ucs->interface->dev,
  1334. "control request 0x%02x failed: %s\n",
  1335. ucs->dr_ctrl.bRequest,
  1336. get_usb_statmsg(status));
  1337. break; /* give up */
  1338. }
  1339. dev_notice(&ucs->interface->dev,
  1340. "control request 0x%02x: %s, retry %d\n",
  1341. ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
  1342. ucs->retry_ctrl);
  1343. /* urb->dev is clobbered by USB subsystem */
  1344. urb->dev = ucs->udev;
  1345. rc = usb_submit_urb(urb, GFP_ATOMIC);
  1346. if (unlikely(rc)) {
  1347. dev_err(&ucs->interface->dev,
  1348. "could not resubmit request 0x%02x: %s\n",
  1349. ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
  1350. break;
  1351. }
  1352. /* resubmitted */
  1353. return;
  1354. }
  1355. /* failed, clear pending request */
  1356. spin_lock_irqsave(&ucs->lock, flags);
  1357. del_timer(&ucs->timer_ctrl);
  1358. ucs->pending = 0;
  1359. spin_unlock_irqrestore(&ucs->lock, flags);
  1360. wake_up(&ucs->waitqueue);
  1361. }
  1362. /* req_submit
  1363. * submit a control output request without message buffer to the Gigaset base
  1364. * and optionally start a timeout
  1365. * parameters:
  1366. * bcs B channel control structure
  1367. * req control request code (HD_*)
  1368. * val control request parameter value (set to 0 if unused)
  1369. * timeout timeout in seconds (0: no timeout)
  1370. * return value:
  1371. * 0 on success
  1372. * -EBUSY if another request is pending
  1373. * any URB submission error code
  1374. */
  1375. static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
  1376. {
  1377. struct bas_cardstate *ucs = bcs->cs->hw.bas;
  1378. int ret;
  1379. unsigned long flags;
  1380. gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
  1381. spin_lock_irqsave(&ucs->lock, flags);
  1382. if (ucs->pending) {
  1383. spin_unlock_irqrestore(&ucs->lock, flags);
  1384. dev_err(bcs->cs->dev,
  1385. "submission of request 0x%02x failed: "
  1386. "request 0x%02x still pending\n",
  1387. req, ucs->pending);
  1388. return -EBUSY;
  1389. }
  1390. ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
  1391. ucs->dr_ctrl.bRequest = req;
  1392. ucs->dr_ctrl.wValue = cpu_to_le16(val);
  1393. ucs->dr_ctrl.wIndex = 0;
  1394. ucs->dr_ctrl.wLength = 0;
  1395. usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
  1396. usb_sndctrlpipe(ucs->udev, 0),
  1397. (unsigned char *) &ucs->dr_ctrl, NULL, 0,
  1398. write_ctrl_callback, ucs);
  1399. ucs->retry_ctrl = 0;
  1400. ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
  1401. if (unlikely(ret)) {
  1402. dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
  1403. req, get_usb_rcmsg(ret));
  1404. spin_unlock_irqrestore(&ucs->lock, flags);
  1405. return ret;
  1406. }
  1407. ucs->pending = req;
  1408. if (timeout > 0) {
  1409. gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
  1410. ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
  1411. ucs->timer_ctrl.data = (unsigned long) bcs;
  1412. ucs->timer_ctrl.function = req_timeout;
  1413. add_timer(&ucs->timer_ctrl);
  1414. }
  1415. spin_unlock_irqrestore(&ucs->lock, flags);
  1416. return 0;
  1417. }
  1418. /* gigaset_init_bchannel
  1419. * called by common.c to connect a B channel
  1420. * initialize isochronous I/O and tell the Gigaset base to open the channel
  1421. * argument:
  1422. * B channel control structure
  1423. * return value:
  1424. * 0 on success, error code < 0 on error
  1425. */
  1426. static int gigaset_init_bchannel(struct bc_state *bcs)
  1427. {
  1428. struct cardstate *cs = bcs->cs;
  1429. int req, ret;
  1430. unsigned long flags;
  1431. spin_lock_irqsave(&cs->lock, flags);
  1432. if (unlikely(!cs->connected)) {
  1433. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1434. spin_unlock_irqrestore(&cs->lock, flags);
  1435. return -ENODEV;
  1436. }
  1437. if (cs->hw.bas->basstate & BS_SUSPEND) {
  1438. dev_notice(cs->dev,
  1439. "not starting isochronous I/O, "
  1440. "suspend in progress\n");
  1441. spin_unlock_irqrestore(&cs->lock, flags);
  1442. return -EHOSTUNREACH;
  1443. }
  1444. ret = starturbs(bcs);
  1445. if (ret < 0) {
  1446. dev_err(cs->dev,
  1447. "could not start isochronous I/O for channel B%d: %s\n",
  1448. bcs->channel + 1,
  1449. ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
  1450. if (ret != -ENODEV)
  1451. error_hangup(bcs);
  1452. spin_unlock_irqrestore(&cs->lock, flags);
  1453. return ret;
  1454. }
  1455. req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
  1456. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1457. if (ret < 0) {
  1458. dev_err(cs->dev, "could not open channel B%d\n",
  1459. bcs->channel + 1);
  1460. stopurbs(bcs->hw.bas);
  1461. if (ret != -ENODEV)
  1462. error_hangup(bcs);
  1463. }
  1464. spin_unlock_irqrestore(&cs->lock, flags);
  1465. return ret;
  1466. }
  1467. /* gigaset_close_bchannel
  1468. * called by common.c to disconnect a B channel
  1469. * tell the Gigaset base to close the channel
  1470. * stopping isochronous I/O and LL notification will be done when the
  1471. * acknowledgement for the close arrives
  1472. * argument:
  1473. * B channel control structure
  1474. * return value:
  1475. * 0 on success, error code < 0 on error
  1476. */
  1477. static int gigaset_close_bchannel(struct bc_state *bcs)
  1478. {
  1479. struct cardstate *cs = bcs->cs;
  1480. int req, ret;
  1481. unsigned long flags;
  1482. spin_lock_irqsave(&cs->lock, flags);
  1483. if (unlikely(!cs->connected)) {
  1484. spin_unlock_irqrestore(&cs->lock, flags);
  1485. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1486. return -ENODEV;
  1487. }
  1488. if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
  1489. /* channel not running: just signal common.c */
  1490. spin_unlock_irqrestore(&cs->lock, flags);
  1491. gigaset_bchannel_down(bcs);
  1492. return 0;
  1493. }
  1494. /* channel running: tell device to close it */
  1495. req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
  1496. ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
  1497. if (ret < 0)
  1498. dev_err(cs->dev, "closing channel B%d failed\n",
  1499. bcs->channel + 1);
  1500. spin_unlock_irqrestore(&cs->lock, flags);
  1501. return ret;
  1502. }
  1503. /* Device Operations */
  1504. /* ================= */
  1505. /* complete_cb
  1506. * unqueue first command buffer from queue, waking any sleepers
  1507. * must be called with cs->cmdlock held
  1508. * parameter:
  1509. * cs controller state structure
  1510. */
  1511. static void complete_cb(struct cardstate *cs)
  1512. {
  1513. struct cmdbuf_t *cb = cs->cmdbuf;
  1514. /* unqueue completed buffer */
  1515. cs->cmdbytes -= cs->curlen;
  1516. gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
  1517. cs->curlen, cs->cmdbytes);
  1518. if (cb->next != NULL) {
  1519. cs->cmdbuf = cb->next;
  1520. cs->cmdbuf->prev = NULL;
  1521. cs->curlen = cs->cmdbuf->len;
  1522. } else {
  1523. cs->cmdbuf = NULL;
  1524. cs->lastcmdbuf = NULL;
  1525. cs->curlen = 0;
  1526. }
  1527. if (cb->wake_tasklet)
  1528. tasklet_schedule(cb->wake_tasklet);
  1529. kfree(cb);
  1530. }
  1531. /* write_command_callback
  1532. * USB completion handler for AT command transmission
  1533. * called by the USB subsystem in interrupt context
  1534. * parameter:
  1535. * urb USB request block of completed request
  1536. * urb->context = controller state structure
  1537. */
  1538. static void write_command_callback(struct urb *urb)
  1539. {
  1540. struct cardstate *cs = urb->context;
  1541. struct bas_cardstate *ucs = cs->hw.bas;
  1542. int status = urb->status;
  1543. unsigned long flags;
  1544. update_basstate(ucs, 0, BS_ATWRPEND);
  1545. wake_up(&ucs->waitqueue);
  1546. /* check status */
  1547. switch (status) {
  1548. case 0: /* normal completion */
  1549. break;
  1550. case -ENOENT: /* cancelled */
  1551. case -ECONNRESET: /* cancelled (async) */
  1552. case -EINPROGRESS: /* pending */
  1553. case -ENODEV: /* device removed */
  1554. case -ESHUTDOWN: /* device shut down */
  1555. /* ignore silently */
  1556. gig_dbg(DEBUG_USBREQ, "%s: %s",
  1557. __func__, get_usb_statmsg(status));
  1558. return;
  1559. default: /* any failure */
  1560. if (++ucs->retry_cmd_out > BAS_RETRY) {
  1561. dev_warn(cs->dev,
  1562. "command write: %s, "
  1563. "giving up after %d retries\n",
  1564. get_usb_statmsg(status),
  1565. ucs->retry_cmd_out);
  1566. break;
  1567. }
  1568. if (ucs->basstate & BS_SUSPEND) {
  1569. dev_warn(cs->dev,
  1570. "command write: %s, "
  1571. "won't retry - suspend requested\n",
  1572. get_usb_statmsg(status));
  1573. break;
  1574. }
  1575. if (cs->cmdbuf == NULL) {
  1576. dev_warn(cs->dev,
  1577. "command write: %s, "
  1578. "cannot retry - cmdbuf gone\n",
  1579. get_usb_statmsg(status));
  1580. break;
  1581. }
  1582. dev_notice(cs->dev, "command write: %s, retry %d\n",
  1583. get_usb_statmsg(status), ucs->retry_cmd_out);
  1584. if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
  1585. /* resubmitted - bypass regular exit block */
  1586. return;
  1587. /* command send failed, assume base still waiting */
  1588. update_basstate(ucs, BS_ATREADY, 0);
  1589. }
  1590. spin_lock_irqsave(&cs->cmdlock, flags);
  1591. if (cs->cmdbuf != NULL)
  1592. complete_cb(cs);
  1593. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1594. }
  1595. /* atrdy_timeout
  1596. * timeout routine for AT command transmission
  1597. * argument:
  1598. * controller state structure
  1599. */
  1600. static void atrdy_timeout(unsigned long data)
  1601. {
  1602. struct cardstate *cs = (struct cardstate *) data;
  1603. struct bas_cardstate *ucs = cs->hw.bas;
  1604. dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
  1605. /* fake the missing signal - what else can I do? */
  1606. update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
  1607. start_cbsend(cs);
  1608. }
  1609. /* atwrite_submit
  1610. * submit an HD_WRITE_ATMESSAGE command URB
  1611. * parameters:
  1612. * cs controller state structure
  1613. * buf buffer containing command to send
  1614. * len length of command to send
  1615. * return value:
  1616. * 0 on success
  1617. * -EBUSY if another request is pending
  1618. * any URB submission error code
  1619. */
  1620. static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
  1621. {
  1622. struct bas_cardstate *ucs = cs->hw.bas;
  1623. int rc;
  1624. gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
  1625. if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
  1626. dev_err(cs->dev,
  1627. "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
  1628. return -EBUSY;
  1629. }
  1630. ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
  1631. ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
  1632. ucs->dr_cmd_out.wValue = 0;
  1633. ucs->dr_cmd_out.wIndex = 0;
  1634. ucs->dr_cmd_out.wLength = cpu_to_le16(len);
  1635. usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
  1636. usb_sndctrlpipe(ucs->udev, 0),
  1637. (unsigned char *) &ucs->dr_cmd_out, buf, len,
  1638. write_command_callback, cs);
  1639. rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
  1640. if (unlikely(rc)) {
  1641. update_basstate(ucs, 0, BS_ATWRPEND);
  1642. dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
  1643. get_usb_rcmsg(rc));
  1644. return rc;
  1645. }
  1646. /* submitted successfully, start timeout if necessary */
  1647. if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
  1648. gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
  1649. ATRDY_TIMEOUT);
  1650. ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
  1651. ucs->timer_atrdy.data = (unsigned long) cs;
  1652. ucs->timer_atrdy.function = atrdy_timeout;
  1653. add_timer(&ucs->timer_atrdy);
  1654. }
  1655. return 0;
  1656. }
  1657. /* start_cbsend
  1658. * start transmission of AT command queue if necessary
  1659. * parameter:
  1660. * cs controller state structure
  1661. * return value:
  1662. * 0 on success
  1663. * error code < 0 on error
  1664. */
  1665. static int start_cbsend(struct cardstate *cs)
  1666. {
  1667. struct cmdbuf_t *cb;
  1668. struct bas_cardstate *ucs = cs->hw.bas;
  1669. unsigned long flags;
  1670. int rc;
  1671. int retval = 0;
  1672. /* check if suspend requested */
  1673. if (ucs->basstate & BS_SUSPEND) {
  1674. gig_dbg(DEBUG_OUTPUT, "suspending");
  1675. return -EHOSTUNREACH;
  1676. }
  1677. /* check if AT channel is open */
  1678. if (!(ucs->basstate & BS_ATOPEN)) {
  1679. gig_dbg(DEBUG_OUTPUT, "AT channel not open");
  1680. rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
  1681. if (rc < 0) {
  1682. /* flush command queue */
  1683. spin_lock_irqsave(&cs->cmdlock, flags);
  1684. while (cs->cmdbuf != NULL)
  1685. complete_cb(cs);
  1686. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1687. }
  1688. return rc;
  1689. }
  1690. /* try to send first command in queue */
  1691. spin_lock_irqsave(&cs->cmdlock, flags);
  1692. while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
  1693. ucs->retry_cmd_out = 0;
  1694. rc = atwrite_submit(cs, cb->buf, cb->len);
  1695. if (unlikely(rc)) {
  1696. retval = rc;
  1697. complete_cb(cs);
  1698. }
  1699. }
  1700. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1701. return retval;
  1702. }
  1703. /* gigaset_write_cmd
  1704. * This function is called by the device independent part of the driver
  1705. * to transmit an AT command string to the Gigaset device.
  1706. * It encapsulates the device specific method for transmission over the
  1707. * direct USB connection to the base.
  1708. * The command string is added to the queue of commands to send, and
  1709. * USB transmission is started if necessary.
  1710. * parameters:
  1711. * cs controller state structure
  1712. * cb command buffer structure
  1713. * return value:
  1714. * number of bytes queued on success
  1715. * error code < 0 on error
  1716. */
  1717. static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
  1718. {
  1719. unsigned long flags;
  1720. int rc;
  1721. gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
  1722. DEBUG_TRANSCMD : DEBUG_LOCKCMD,
  1723. "CMD Transmit", cb->len, cb->buf);
  1724. /* translate "+++" escape sequence sent as a single separate command
  1725. * into "close AT channel" command for error recovery
  1726. * The next command will reopen the AT channel automatically.
  1727. */
  1728. if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
  1729. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
  1730. if (cb->wake_tasklet)
  1731. tasklet_schedule(cb->wake_tasklet);
  1732. if (!rc)
  1733. rc = cb->len;
  1734. kfree(cb);
  1735. return rc;
  1736. }
  1737. spin_lock_irqsave(&cs->cmdlock, flags);
  1738. cb->prev = cs->lastcmdbuf;
  1739. if (cs->lastcmdbuf)
  1740. cs->lastcmdbuf->next = cb;
  1741. else {
  1742. cs->cmdbuf = cb;
  1743. cs->curlen = cb->len;
  1744. }
  1745. cs->cmdbytes += cb->len;
  1746. cs->lastcmdbuf = cb;
  1747. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1748. spin_lock_irqsave(&cs->lock, flags);
  1749. if (unlikely(!cs->connected)) {
  1750. spin_unlock_irqrestore(&cs->lock, flags);
  1751. gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
  1752. /* flush command queue */
  1753. spin_lock_irqsave(&cs->cmdlock, flags);
  1754. while (cs->cmdbuf != NULL)
  1755. complete_cb(cs);
  1756. spin_unlock_irqrestore(&cs->cmdlock, flags);
  1757. return -ENODEV;
  1758. }
  1759. rc = start_cbsend(cs);
  1760. spin_unlock_irqrestore(&cs->lock, flags);
  1761. return rc < 0 ? rc : cb->len;
  1762. }
  1763. /* gigaset_write_room
  1764. * tty_driver.write_room interface routine
  1765. * return number of characters the driver will accept to be written via
  1766. * gigaset_write_cmd
  1767. * parameter:
  1768. * controller state structure
  1769. * return value:
  1770. * number of characters
  1771. */
  1772. static int gigaset_write_room(struct cardstate *cs)
  1773. {
  1774. return IF_WRITEBUF;
  1775. }
  1776. /* gigaset_chars_in_buffer
  1777. * tty_driver.chars_in_buffer interface routine
  1778. * return number of characters waiting to be sent
  1779. * parameter:
  1780. * controller state structure
  1781. * return value:
  1782. * number of characters
  1783. */
  1784. static int gigaset_chars_in_buffer(struct cardstate *cs)
  1785. {
  1786. return cs->cmdbytes;
  1787. }
  1788. /* gigaset_brkchars
  1789. * implementation of ioctl(GIGASET_BRKCHARS)
  1790. * parameter:
  1791. * controller state structure
  1792. * return value:
  1793. * -EINVAL (unimplemented function)
  1794. */
  1795. static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
  1796. {
  1797. return -EINVAL;
  1798. }
  1799. /* Device Initialization/Shutdown */
  1800. /* ============================== */
  1801. /* Free hardware dependent part of the B channel structure
  1802. * parameter:
  1803. * bcs B channel structure
  1804. * return value:
  1805. * !=0 on success
  1806. */
  1807. static int gigaset_freebcshw(struct bc_state *bcs)
  1808. {
  1809. struct bas_bc_state *ubc = bcs->hw.bas;
  1810. int i;
  1811. if (!ubc)
  1812. return 0;
  1813. /* kill URBs and tasklets before freeing - better safe than sorry */
  1814. ubc->running = 0;
  1815. gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
  1816. for (i = 0; i < BAS_OUTURBS; ++i) {
  1817. usb_kill_urb(ubc->isoouturbs[i].urb);
  1818. usb_free_urb(ubc->isoouturbs[i].urb);
  1819. }
  1820. for (i = 0; i < BAS_INURBS; ++i) {
  1821. usb_kill_urb(ubc->isoinurbs[i]);
  1822. usb_free_urb(ubc->isoinurbs[i]);
  1823. }
  1824. tasklet_kill(&ubc->sent_tasklet);
  1825. tasklet_kill(&ubc->rcvd_tasklet);
  1826. kfree(ubc->isooutbuf);
  1827. kfree(ubc);
  1828. bcs->hw.bas = NULL;
  1829. return 1;
  1830. }
  1831. /* Initialize hardware dependent part of the B channel structure
  1832. * parameter:
  1833. * bcs B channel structure
  1834. * return value:
  1835. * !=0 on success
  1836. */
  1837. static int gigaset_initbcshw(struct bc_state *bcs)
  1838. {
  1839. int i;
  1840. struct bas_bc_state *ubc;
  1841. bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
  1842. if (!ubc) {
  1843. pr_err("out of memory\n");
  1844. return 0;
  1845. }
  1846. ubc->running = 0;
  1847. atomic_set(&ubc->corrbytes, 0);
  1848. spin_lock_init(&ubc->isooutlock);
  1849. for (i = 0; i < BAS_OUTURBS; ++i) {
  1850. ubc->isoouturbs[i].urb = NULL;
  1851. ubc->isoouturbs[i].bcs = bcs;
  1852. }
  1853. ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
  1854. ubc->numsub = 0;
  1855. ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
  1856. if (!ubc->isooutbuf) {
  1857. pr_err("out of memory\n");
  1858. kfree(ubc);
  1859. bcs->hw.bas = NULL;
  1860. return 0;
  1861. }
  1862. tasklet_init(&ubc->sent_tasklet,
  1863. write_iso_tasklet, (unsigned long) bcs);
  1864. spin_lock_init(&ubc->isoinlock);
  1865. for (i = 0; i < BAS_INURBS; ++i)
  1866. ubc->isoinurbs[i] = NULL;
  1867. ubc->isoindone = NULL;
  1868. ubc->loststatus = -EINPROGRESS;
  1869. ubc->isoinlost = 0;
  1870. ubc->seqlen = 0;
  1871. ubc->inbyte = 0;
  1872. ubc->inbits = 0;
  1873. ubc->goodbytes = 0;
  1874. ubc->alignerrs = 0;
  1875. ubc->fcserrs = 0;
  1876. ubc->frameerrs = 0;
  1877. ubc->giants = 0;
  1878. ubc->runts = 0;
  1879. ubc->aborts = 0;
  1880. ubc->shared0s = 0;
  1881. ubc->stolen0s = 0;
  1882. tasklet_init(&ubc->rcvd_tasklet,
  1883. read_iso_tasklet, (unsigned long) bcs);
  1884. return 1;
  1885. }
  1886. static void gigaset_reinitbcshw(struct bc_state *bcs)
  1887. {
  1888. struct bas_bc_state *ubc = bcs->hw.bas;
  1889. bcs->hw.bas->running = 0;
  1890. atomic_set(&bcs->hw.bas->corrbytes, 0);
  1891. bcs->hw.bas->numsub = 0;
  1892. spin_lock_init(&ubc->isooutlock);
  1893. spin_lock_init(&ubc->isoinlock);
  1894. ubc->loststatus = -EINPROGRESS;
  1895. }
  1896. static void gigaset_freecshw(struct cardstate *cs)
  1897. {
  1898. /* timers, URBs and rcvbuf are disposed of in disconnect */
  1899. kfree(cs->hw.bas->int_in_buf);
  1900. kfree(cs->hw.bas);
  1901. cs->hw.bas = NULL;
  1902. }
  1903. static int gigaset_initcshw(struct cardstate *cs)
  1904. {
  1905. struct bas_cardstate *ucs;
  1906. cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
  1907. if (!ucs) {
  1908. pr_err("out of memory\n");
  1909. return 0;
  1910. }
  1911. ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
  1912. if (!ucs->int_in_buf) {
  1913. kfree(ucs);
  1914. pr_err("out of memory\n");
  1915. return 0;
  1916. }
  1917. ucs->urb_cmd_in = NULL;
  1918. ucs->urb_cmd_out = NULL;
  1919. ucs->rcvbuf = NULL;
  1920. ucs->rcvbuf_size = 0;
  1921. spin_lock_init(&ucs->lock);
  1922. ucs->pending = 0;
  1923. ucs->basstate = 0;
  1924. init_timer(&ucs->timer_ctrl);
  1925. init_timer(&ucs->timer_atrdy);
  1926. init_timer(&ucs->timer_cmd_in);
  1927. init_waitqueue_head(&ucs->waitqueue);
  1928. return 1;
  1929. }
  1930. /* freeurbs
  1931. * unlink and deallocate all URBs unconditionally
  1932. * caller must make sure that no commands are still in progress
  1933. * parameter:
  1934. * cs controller state structure
  1935. */
  1936. static void freeurbs(struct cardstate *cs)
  1937. {
  1938. struct bas_cardstate *ucs = cs->hw.bas;
  1939. struct bas_bc_state *ubc;
  1940. int i, j;
  1941. gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
  1942. for (j = 0; j < BAS_CHANNELS; ++j) {
  1943. ubc = cs->bcs[j].hw.bas;
  1944. for (i = 0; i < BAS_OUTURBS; ++i) {
  1945. usb_kill_urb(ubc->isoouturbs[i].urb);
  1946. usb_free_urb(ubc->isoouturbs[i].urb);
  1947. ubc->isoouturbs[i].urb = NULL;
  1948. }
  1949. for (i = 0; i < BAS_INURBS; ++i) {
  1950. usb_kill_urb(ubc->isoinurbs[i]);
  1951. usb_free_urb(ubc->isoinurbs[i]);
  1952. ubc->isoinurbs[i] = NULL;
  1953. }
  1954. }
  1955. usb_kill_urb(ucs->urb_int_in);
  1956. usb_free_urb(ucs->urb_int_in);
  1957. ucs->urb_int_in = NULL;
  1958. usb_kill_urb(ucs->urb_cmd_out);
  1959. usb_free_urb(ucs->urb_cmd_out);
  1960. ucs->urb_cmd_out = NULL;
  1961. usb_kill_urb(ucs->urb_cmd_in);
  1962. usb_free_urb(ucs->urb_cmd_in);
  1963. ucs->urb_cmd_in = NULL;
  1964. usb_kill_urb(ucs->urb_ctrl);
  1965. usb_free_urb(ucs->urb_ctrl);
  1966. ucs->urb_ctrl = NULL;
  1967. }
  1968. /* gigaset_probe
  1969. * This function is called when a new USB device is connected.
  1970. * It checks whether the new device is handled by this driver.
  1971. */
  1972. static int gigaset_probe(struct usb_interface *interface,
  1973. const struct usb_device_id *id)
  1974. {
  1975. struct usb_host_interface *hostif;
  1976. struct usb_device *udev = interface_to_usbdev(interface);
  1977. struct cardstate *cs = NULL;
  1978. struct bas_cardstate *ucs = NULL;
  1979. struct bas_bc_state *ubc;
  1980. struct usb_endpoint_descriptor *endpoint;
  1981. int i, j;
  1982. int rc;
  1983. gig_dbg(DEBUG_INIT,
  1984. "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
  1985. __func__, le16_to_cpu(udev->descriptor.idVendor),
  1986. le16_to_cpu(udev->descriptor.idProduct));
  1987. /* set required alternate setting */
  1988. hostif = interface->cur_altsetting;
  1989. if (hostif->desc.bAlternateSetting != 3) {
  1990. gig_dbg(DEBUG_INIT,
  1991. "%s: wrong alternate setting %d - trying to switch",
  1992. __func__, hostif->desc.bAlternateSetting);
  1993. if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
  1994. < 0) {
  1995. dev_warn(&udev->dev, "usb_set_interface failed, "
  1996. "device %d interface %d altsetting %d\n",
  1997. udev->devnum, hostif->desc.bInterfaceNumber,
  1998. hostif->desc.bAlternateSetting);
  1999. return -ENODEV;
  2000. }
  2001. hostif = interface->cur_altsetting;
  2002. }
  2003. /* Reject application specific interfaces
  2004. */
  2005. if (hostif->desc.bInterfaceClass != 255) {
  2006. dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
  2007. __func__, hostif->desc.bInterfaceClass);
  2008. return -ENODEV;
  2009. }
  2010. dev_info(&udev->dev,
  2011. "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
  2012. __func__, le16_to_cpu(udev->descriptor.idVendor),
  2013. le16_to_cpu(udev->descriptor.idProduct));
  2014. /* allocate memory for our device state and intialize it */
  2015. cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
  2016. GIGASET_MODULENAME);
  2017. if (!cs)
  2018. return -ENODEV;
  2019. ucs = cs->hw.bas;
  2020. /* save off device structure ptrs for later use */
  2021. usb_get_dev(udev);
  2022. ucs->udev = udev;
  2023. ucs->interface = interface;
  2024. cs->dev = &interface->dev;
  2025. /* allocate URBs:
  2026. * - one for the interrupt pipe
  2027. * - three for the different uses of the default control pipe
  2028. * - three for each isochronous pipe
  2029. */
  2030. if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2031. !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
  2032. !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
  2033. !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  2034. goto allocerr;
  2035. for (j = 0; j < BAS_CHANNELS; ++j) {
  2036. ubc = cs->bcs[j].hw.bas;
  2037. for (i = 0; i < BAS_OUTURBS; ++i)
  2038. if (!(ubc->isoouturbs[i].urb =
  2039. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2040. goto allocerr;
  2041. for (i = 0; i < BAS_INURBS; ++i)
  2042. if (!(ubc->isoinurbs[i] =
  2043. usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
  2044. goto allocerr;
  2045. }
  2046. ucs->rcvbuf = NULL;
  2047. ucs->rcvbuf_size = 0;
  2048. /* Fill the interrupt urb and send it to the core */
  2049. endpoint = &hostif->endpoint[0].desc;
  2050. usb_fill_int_urb(ucs->urb_int_in, udev,
  2051. usb_rcvintpipe(udev,
  2052. (endpoint->bEndpointAddress) & 0x0f),
  2053. ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
  2054. endpoint->bInterval);
  2055. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2056. if (rc != 0) {
  2057. dev_err(cs->dev, "could not submit interrupt URB: %s\n",
  2058. get_usb_rcmsg(rc));
  2059. goto error;
  2060. }
  2061. /* tell the device that the driver is ready */
  2062. rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
  2063. if (rc != 0)
  2064. goto error;
  2065. /* tell common part that the device is ready */
  2066. if (startmode == SM_LOCKED)
  2067. cs->mstate = MS_LOCKED;
  2068. /* save address of controller structure */
  2069. usb_set_intfdata(interface, cs);
  2070. if (!gigaset_start(cs))
  2071. goto error;
  2072. return 0;
  2073. allocerr:
  2074. dev_err(cs->dev, "could not allocate URBs\n");
  2075. error:
  2076. freeurbs(cs);
  2077. usb_set_intfdata(interface, NULL);
  2078. gigaset_freecs(cs);
  2079. return -ENODEV;
  2080. }
  2081. /* gigaset_disconnect
  2082. * This function is called when the Gigaset base is unplugged.
  2083. */
  2084. static void gigaset_disconnect(struct usb_interface *interface)
  2085. {
  2086. struct cardstate *cs;
  2087. struct bas_cardstate *ucs;
  2088. int j;
  2089. cs = usb_get_intfdata(interface);
  2090. ucs = cs->hw.bas;
  2091. dev_info(cs->dev, "disconnecting Gigaset base\n");
  2092. /* mark base as not ready, all channels disconnected */
  2093. ucs->basstate = 0;
  2094. /* tell LL all channels are down */
  2095. for (j = 0; j < BAS_CHANNELS; ++j)
  2096. gigaset_bchannel_down(cs->bcs + j);
  2097. /* stop driver (common part) */
  2098. gigaset_stop(cs);
  2099. /* stop timers and URBs, free ressources */
  2100. del_timer_sync(&ucs->timer_ctrl);
  2101. del_timer_sync(&ucs->timer_atrdy);
  2102. del_timer_sync(&ucs->timer_cmd_in);
  2103. freeurbs(cs);
  2104. usb_set_intfdata(interface, NULL);
  2105. kfree(ucs->rcvbuf);
  2106. ucs->rcvbuf = NULL;
  2107. ucs->rcvbuf_size = 0;
  2108. usb_put_dev(ucs->udev);
  2109. ucs->interface = NULL;
  2110. ucs->udev = NULL;
  2111. cs->dev = NULL;
  2112. gigaset_freecs(cs);
  2113. }
  2114. /* gigaset_suspend
  2115. * This function is called before the USB connection is suspended.
  2116. */
  2117. static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
  2118. {
  2119. struct cardstate *cs = usb_get_intfdata(intf);
  2120. struct bas_cardstate *ucs = cs->hw.bas;
  2121. int rc;
  2122. /* set suspend flag; this stops AT command/response traffic */
  2123. if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
  2124. gig_dbg(DEBUG_SUSPEND, "already suspended");
  2125. return 0;
  2126. }
  2127. /* wait a bit for blocking conditions to go away */
  2128. rc = wait_event_timeout(ucs->waitqueue,
  2129. !(ucs->basstate &
  2130. (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
  2131. BAS_TIMEOUT*HZ/10);
  2132. gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
  2133. /* check for conditions preventing suspend */
  2134. if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
  2135. dev_warn(cs->dev, "cannot suspend:\n");
  2136. if (ucs->basstate & BS_B1OPEN)
  2137. dev_warn(cs->dev, " B channel 1 open\n");
  2138. if (ucs->basstate & BS_B2OPEN)
  2139. dev_warn(cs->dev, " B channel 2 open\n");
  2140. if (ucs->basstate & BS_ATRDPEND)
  2141. dev_warn(cs->dev, " receiving AT reply\n");
  2142. if (ucs->basstate & BS_ATWRPEND)
  2143. dev_warn(cs->dev, " sending AT command\n");
  2144. update_basstate(ucs, 0, BS_SUSPEND);
  2145. return -EBUSY;
  2146. }
  2147. /* close AT channel if open */
  2148. if (ucs->basstate & BS_ATOPEN) {
  2149. gig_dbg(DEBUG_SUSPEND, "closing AT channel");
  2150. rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
  2151. if (rc) {
  2152. update_basstate(ucs, 0, BS_SUSPEND);
  2153. return rc;
  2154. }
  2155. wait_event_timeout(ucs->waitqueue, !ucs->pending,
  2156. BAS_TIMEOUT*HZ/10);
  2157. /* in case of timeout, proceed anyway */
  2158. }
  2159. /* kill all URBs and timers that might still be pending */
  2160. usb_kill_urb(ucs->urb_ctrl);
  2161. usb_kill_urb(ucs->urb_int_in);
  2162. del_timer_sync(&ucs->timer_ctrl);
  2163. gig_dbg(DEBUG_SUSPEND, "suspend complete");
  2164. return 0;
  2165. }
  2166. /* gigaset_resume
  2167. * This function is called after the USB connection has been resumed.
  2168. */
  2169. static int gigaset_resume(struct usb_interface *intf)
  2170. {
  2171. struct cardstate *cs = usb_get_intfdata(intf);
  2172. struct bas_cardstate *ucs = cs->hw.bas;
  2173. int rc;
  2174. /* resubmit interrupt URB for spontaneous messages from base */
  2175. rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
  2176. if (rc) {
  2177. dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
  2178. get_usb_rcmsg(rc));
  2179. return rc;
  2180. }
  2181. /* clear suspend flag to reallow activity */
  2182. update_basstate(ucs, 0, BS_SUSPEND);
  2183. gig_dbg(DEBUG_SUSPEND, "resume complete");
  2184. return 0;
  2185. }
  2186. /* gigaset_pre_reset
  2187. * This function is called before the USB connection is reset.
  2188. */
  2189. static int gigaset_pre_reset(struct usb_interface *intf)
  2190. {
  2191. /* handle just like suspend */
  2192. return gigaset_suspend(intf, PMSG_ON);
  2193. }
  2194. /* gigaset_post_reset
  2195. * This function is called after the USB connection has been reset.
  2196. */
  2197. static int gigaset_post_reset(struct usb_interface *intf)
  2198. {
  2199. /* FIXME: send HD_DEVICE_INIT_ACK? */
  2200. /* resume operations */
  2201. return gigaset_resume(intf);
  2202. }
  2203. static const struct gigaset_ops gigops = {
  2204. gigaset_write_cmd,
  2205. gigaset_write_room,
  2206. gigaset_chars_in_buffer,
  2207. gigaset_brkchars,
  2208. gigaset_init_bchannel,
  2209. gigaset_close_bchannel,
  2210. gigaset_initbcshw,
  2211. gigaset_freebcshw,
  2212. gigaset_reinitbcshw,
  2213. gigaset_initcshw,
  2214. gigaset_freecshw,
  2215. gigaset_set_modem_ctrl,
  2216. gigaset_baud_rate,
  2217. gigaset_set_line_ctrl,
  2218. gigaset_isoc_send_skb,
  2219. gigaset_isoc_input,
  2220. };
  2221. /* bas_gigaset_init
  2222. * This function is called after the kernel module is loaded.
  2223. */
  2224. static int __init bas_gigaset_init(void)
  2225. {
  2226. int result;
  2227. /* allocate memory for our driver state and intialize it */
  2228. driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
  2229. GIGASET_MODULENAME, GIGASET_DEVNAME,
  2230. &gigops, THIS_MODULE);
  2231. if (driver == NULL)
  2232. goto error;
  2233. /* register this driver with the USB subsystem */
  2234. result = usb_register(&gigaset_usb_driver);
  2235. if (result < 0) {
  2236. pr_err("error %d registering USB driver\n", -result);
  2237. goto error;
  2238. }
  2239. pr_info(DRIVER_DESC "\n");
  2240. return 0;
  2241. error:
  2242. if (driver)
  2243. gigaset_freedriver(driver);
  2244. driver = NULL;
  2245. return -1;
  2246. }
  2247. /* bas_gigaset_exit
  2248. * This function is called before the kernel module is unloaded.
  2249. */
  2250. static void __exit bas_gigaset_exit(void)
  2251. {
  2252. struct bas_cardstate *ucs;
  2253. int i;
  2254. gigaset_blockdriver(driver); /* => probe will fail
  2255. * => no gigaset_start any more
  2256. */
  2257. /* stop all connected devices */
  2258. for (i = 0; i < driver->minors; i++) {
  2259. if (gigaset_shutdown(driver->cs + i) < 0)
  2260. continue; /* no device */
  2261. /* from now on, no isdn callback should be possible */
  2262. /* close all still open channels */
  2263. ucs = driver->cs[i].hw.bas;
  2264. if (ucs->basstate & BS_B1OPEN) {
  2265. gig_dbg(DEBUG_INIT, "closing B1 channel");
  2266. usb_control_msg(ucs->udev,
  2267. usb_sndctrlpipe(ucs->udev, 0),
  2268. HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
  2269. 0, 0, NULL, 0, BAS_TIMEOUT);
  2270. }
  2271. if (ucs->basstate & BS_B2OPEN) {
  2272. gig_dbg(DEBUG_INIT, "closing B2 channel");
  2273. usb_control_msg(ucs->udev,
  2274. usb_sndctrlpipe(ucs->udev, 0),
  2275. HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
  2276. 0, 0, NULL, 0, BAS_TIMEOUT);
  2277. }
  2278. if (ucs->basstate & BS_ATOPEN) {
  2279. gig_dbg(DEBUG_INIT, "closing AT channel");
  2280. usb_control_msg(ucs->udev,
  2281. usb_sndctrlpipe(ucs->udev, 0),
  2282. HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
  2283. 0, 0, NULL, 0, BAS_TIMEOUT);
  2284. }
  2285. ucs->basstate = 0;
  2286. }
  2287. /* deregister this driver with the USB subsystem */
  2288. usb_deregister(&gigaset_usb_driver);
  2289. /* this will call the disconnect-callback */
  2290. /* from now on, no disconnect/probe callback should be running */
  2291. gigaset_freedriver(driver);
  2292. driver = NULL;
  2293. }
  2294. module_init(bas_gigaset_init);
  2295. module_exit(bas_gigaset_exit);
  2296. MODULE_AUTHOR(DRIVER_AUTHOR);
  2297. MODULE_DESCRIPTION(DRIVER_DESC);
  2298. MODULE_LICENSE("GPL");