hso.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345
  1. /******************************************************************************
  2. *
  3. * Driver for Option High Speed Mobile Devices.
  4. *
  5. * Copyright (C) 2008 Option International
  6. * Filip Aben <f.aben@option.com>
  7. * Denis Joseph Barrow <d.barow@option.com>
  8. * Copyright (C) 2007 Andrew Bird (Sphere Systems Ltd)
  9. * <ajb@spheresystems.co.uk>
  10. * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  11. * Copyright (C) 2008 Novell, Inc.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  25. * USA
  26. *
  27. *
  28. *****************************************************************************/
  29. /******************************************************************************
  30. *
  31. * Description of the device:
  32. *
  33. * Interface 0: Contains the IP network interface on the bulk end points.
  34. * The multiplexed serial ports are using the interrupt and
  35. * control endpoints.
  36. * Interrupt contains a bitmap telling which multiplexed
  37. * serialport needs servicing.
  38. *
  39. * Interface 1: Diagnostics port, uses bulk only, do not submit urbs until the
  40. * port is opened, as this have a huge impact on the network port
  41. * throughput.
  42. *
  43. * Interface 2: Standard modem interface - circuit switched interface, this
  44. * can be used to make a standard ppp connection however it
  45. * should not be used in conjunction with the IP network interface
  46. * enabled for USB performance reasons i.e. if using this set
  47. * ideally disable_net=1.
  48. *
  49. *****************************************************************************/
  50. #include <linux/sched.h>
  51. #include <linux/slab.h>
  52. #include <linux/init.h>
  53. #include <linux/delay.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/module.h>
  56. #include <linux/ethtool.h>
  57. #include <linux/usb.h>
  58. #include <linux/timer.h>
  59. #include <linux/tty.h>
  60. #include <linux/tty_driver.h>
  61. #include <linux/tty_flip.h>
  62. #include <linux/kmod.h>
  63. #include <linux/rfkill.h>
  64. #include <linux/ip.h>
  65. #include <linux/uaccess.h>
  66. #include <linux/usb/cdc.h>
  67. #include <net/arp.h>
  68. #include <asm/byteorder.h>
  69. #include <linux/serial_core.h>
  70. #include <linux/serial.h>
  71. #define DRIVER_VERSION "1.2"
  72. #define MOD_AUTHOR "Option Wireless"
  73. #define MOD_DESCRIPTION "USB High Speed Option driver"
  74. #define MOD_LICENSE "GPL"
  75. #define HSO_MAX_NET_DEVICES 10
  76. #define HSO__MAX_MTU 2048
  77. #define DEFAULT_MTU 1500
  78. #define DEFAULT_MRU 1500
  79. #define CTRL_URB_RX_SIZE 1024
  80. #define CTRL_URB_TX_SIZE 64
  81. #define BULK_URB_RX_SIZE 4096
  82. #define BULK_URB_TX_SIZE 8192
  83. #define MUX_BULK_RX_BUF_SIZE HSO__MAX_MTU
  84. #define MUX_BULK_TX_BUF_SIZE HSO__MAX_MTU
  85. #define MUX_BULK_RX_BUF_COUNT 4
  86. #define USB_TYPE_OPTION_VENDOR 0x20
  87. /* These definitions are used with the struct hso_net flags element */
  88. /* - use *_bit operations on it. (bit indices not values.) */
  89. #define HSO_NET_RUNNING 0
  90. #define HSO_NET_TX_TIMEOUT (HZ*10)
  91. #define HSO_SERIAL_MAGIC 0x48534f31
  92. /* Number of ttys to handle */
  93. #define HSO_SERIAL_TTY_MINORS 256
  94. #define MAX_RX_URBS 2
  95. static inline struct hso_serial *get_serial_by_tty(struct tty_struct *tty)
  96. {
  97. if (tty)
  98. return tty->driver_data;
  99. return NULL;
  100. }
  101. /*****************************************************************************/
  102. /* Debugging functions */
  103. /*****************************************************************************/
  104. #define D__(lvl_, fmt, arg...) \
  105. do { \
  106. printk(lvl_ "[%d:%s]: " fmt "\n", \
  107. __LINE__, __func__, ## arg); \
  108. } while (0)
  109. #define D_(lvl, args...) \
  110. do { \
  111. if (lvl & debug) \
  112. D__(KERN_INFO, args); \
  113. } while (0)
  114. #define D1(args...) D_(0x01, ##args)
  115. #define D2(args...) D_(0x02, ##args)
  116. #define D3(args...) D_(0x04, ##args)
  117. #define D4(args...) D_(0x08, ##args)
  118. #define D5(args...) D_(0x10, ##args)
  119. /*****************************************************************************/
  120. /* Enumerators */
  121. /*****************************************************************************/
  122. enum pkt_parse_state {
  123. WAIT_IP,
  124. WAIT_DATA,
  125. WAIT_SYNC
  126. };
  127. /*****************************************************************************/
  128. /* Structs */
  129. /*****************************************************************************/
  130. struct hso_shared_int {
  131. struct usb_endpoint_descriptor *intr_endp;
  132. void *shared_intr_buf;
  133. struct urb *shared_intr_urb;
  134. struct usb_device *usb;
  135. int use_count;
  136. int ref_count;
  137. struct mutex shared_int_lock;
  138. };
  139. struct hso_net {
  140. struct hso_device *parent;
  141. struct net_device *net;
  142. struct rfkill *rfkill;
  143. struct usb_endpoint_descriptor *in_endp;
  144. struct usb_endpoint_descriptor *out_endp;
  145. struct urb *mux_bulk_rx_urb_pool[MUX_BULK_RX_BUF_COUNT];
  146. struct urb *mux_bulk_tx_urb;
  147. void *mux_bulk_rx_buf_pool[MUX_BULK_RX_BUF_COUNT];
  148. void *mux_bulk_tx_buf;
  149. struct sk_buff *skb_rx_buf;
  150. struct sk_buff *skb_tx_buf;
  151. enum pkt_parse_state rx_parse_state;
  152. spinlock_t net_lock;
  153. unsigned short rx_buf_size;
  154. unsigned short rx_buf_missing;
  155. struct iphdr rx_ip_hdr;
  156. unsigned long flags;
  157. };
  158. enum rx_ctrl_state{
  159. RX_IDLE,
  160. RX_SENT,
  161. RX_PENDING
  162. };
  163. #define BM_REQUEST_TYPE (0xa1)
  164. #define B_NOTIFICATION (0x20)
  165. #define W_VALUE (0x0)
  166. #define W_INDEX (0x2)
  167. #define W_LENGTH (0x2)
  168. #define B_OVERRUN (0x1<<6)
  169. #define B_PARITY (0x1<<5)
  170. #define B_FRAMING (0x1<<4)
  171. #define B_RING_SIGNAL (0x1<<3)
  172. #define B_BREAK (0x1<<2)
  173. #define B_TX_CARRIER (0x1<<1)
  174. #define B_RX_CARRIER (0x1<<0)
  175. struct hso_serial_state_notification {
  176. u8 bmRequestType;
  177. u8 bNotification;
  178. u16 wValue;
  179. u16 wIndex;
  180. u16 wLength;
  181. u16 UART_state_bitmap;
  182. } __attribute__((packed));
  183. struct hso_tiocmget {
  184. struct mutex mutex;
  185. wait_queue_head_t waitq;
  186. int intr_completed;
  187. struct usb_endpoint_descriptor *endp;
  188. struct urb *urb;
  189. struct hso_serial_state_notification serial_state_notification;
  190. u16 prev_UART_state_bitmap;
  191. struct uart_icount icount;
  192. };
  193. struct hso_serial {
  194. struct hso_device *parent;
  195. int magic;
  196. u8 minor;
  197. struct hso_shared_int *shared_int;
  198. /* rx/tx urb could be either a bulk urb or a control urb depending
  199. on which serial port it is used on. */
  200. struct urb *rx_urb[MAX_RX_URBS];
  201. u8 num_rx_urbs;
  202. u8 *rx_data[MAX_RX_URBS];
  203. u16 rx_data_length; /* should contain allocated length */
  204. struct urb *tx_urb;
  205. u8 *tx_data;
  206. u8 *tx_buffer;
  207. u16 tx_data_length; /* should contain allocated length */
  208. u16 tx_data_count;
  209. u16 tx_buffer_count;
  210. struct usb_ctrlrequest ctrl_req_tx;
  211. struct usb_ctrlrequest ctrl_req_rx;
  212. struct usb_endpoint_descriptor *in_endp;
  213. struct usb_endpoint_descriptor *out_endp;
  214. enum rx_ctrl_state rx_state;
  215. u8 rts_state;
  216. u8 dtr_state;
  217. unsigned tx_urb_used:1;
  218. /* from usb_serial_port */
  219. struct tty_struct *tty;
  220. int open_count;
  221. spinlock_t serial_lock;
  222. int (*write_data) (struct hso_serial *serial);
  223. struct hso_tiocmget *tiocmget;
  224. /* Hacks required to get flow control
  225. * working on the serial receive buffers
  226. * so as not to drop characters on the floor.
  227. */
  228. int curr_rx_urb_idx;
  229. u16 curr_rx_urb_offset;
  230. u8 rx_urb_filled[MAX_RX_URBS];
  231. struct tasklet_struct unthrottle_tasklet;
  232. struct work_struct retry_unthrottle_workqueue;
  233. };
  234. struct hso_device {
  235. union {
  236. struct hso_serial *dev_serial;
  237. struct hso_net *dev_net;
  238. } port_data;
  239. u32 port_spec;
  240. u8 is_active;
  241. u8 usb_gone;
  242. struct work_struct async_get_intf;
  243. struct work_struct async_put_intf;
  244. struct usb_device *usb;
  245. struct usb_interface *interface;
  246. struct device *dev;
  247. struct kref ref;
  248. struct mutex mutex;
  249. };
  250. /* Type of interface */
  251. #define HSO_INTF_MASK 0xFF00
  252. #define HSO_INTF_MUX 0x0100
  253. #define HSO_INTF_BULK 0x0200
  254. /* Type of port */
  255. #define HSO_PORT_MASK 0xFF
  256. #define HSO_PORT_NO_PORT 0x0
  257. #define HSO_PORT_CONTROL 0x1
  258. #define HSO_PORT_APP 0x2
  259. #define HSO_PORT_GPS 0x3
  260. #define HSO_PORT_PCSC 0x4
  261. #define HSO_PORT_APP2 0x5
  262. #define HSO_PORT_GPS_CONTROL 0x6
  263. #define HSO_PORT_MSD 0x7
  264. #define HSO_PORT_VOICE 0x8
  265. #define HSO_PORT_DIAG2 0x9
  266. #define HSO_PORT_DIAG 0x10
  267. #define HSO_PORT_MODEM 0x11
  268. #define HSO_PORT_NETWORK 0x12
  269. /* Additional device info */
  270. #define HSO_INFO_MASK 0xFF000000
  271. #define HSO_INFO_CRC_BUG 0x01000000
  272. /*****************************************************************************/
  273. /* Prototypes */
  274. /*****************************************************************************/
  275. /* Serial driver functions */
  276. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  277. unsigned int set, unsigned int clear);
  278. static void ctrl_callback(struct urb *urb);
  279. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial);
  280. static void hso_kick_transmit(struct hso_serial *serial);
  281. /* Helper functions */
  282. static int hso_mux_submit_intr_urb(struct hso_shared_int *mux_int,
  283. struct usb_device *usb, gfp_t gfp);
  284. static void log_usb_status(int status, const char *function);
  285. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  286. int type, int dir);
  287. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports);
  288. static void hso_free_interface(struct usb_interface *intf);
  289. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags);
  290. static int hso_stop_serial_device(struct hso_device *hso_dev);
  291. static int hso_start_net_device(struct hso_device *hso_dev);
  292. static void hso_free_shared_int(struct hso_shared_int *shared_int);
  293. static int hso_stop_net_device(struct hso_device *hso_dev);
  294. static void hso_serial_ref_free(struct kref *ref);
  295. static void hso_std_serial_read_bulk_callback(struct urb *urb);
  296. static int hso_mux_serial_read(struct hso_serial *serial);
  297. static void async_get_intf(struct work_struct *data);
  298. static void async_put_intf(struct work_struct *data);
  299. static int hso_put_activity(struct hso_device *hso_dev);
  300. static int hso_get_activity(struct hso_device *hso_dev);
  301. static void tiocmget_intr_callback(struct urb *urb);
  302. /*****************************************************************************/
  303. /* Helping functions */
  304. /*****************************************************************************/
  305. /* #define DEBUG */
  306. static inline struct hso_net *dev2net(struct hso_device *hso_dev)
  307. {
  308. return hso_dev->port_data.dev_net;
  309. }
  310. static inline struct hso_serial *dev2ser(struct hso_device *hso_dev)
  311. {
  312. return hso_dev->port_data.dev_serial;
  313. }
  314. /* Debugging functions */
  315. #ifdef DEBUG
  316. static void dbg_dump(int line_count, const char *func_name, unsigned char *buf,
  317. unsigned int len)
  318. {
  319. static char name[255];
  320. sprintf(name, "hso[%d:%s]", line_count, func_name);
  321. print_hex_dump_bytes(name, DUMP_PREFIX_NONE, buf, len);
  322. }
  323. #define DUMP(buf_, len_) \
  324. dbg_dump(__LINE__, __func__, buf_, len_)
  325. #define DUMP1(buf_, len_) \
  326. do { \
  327. if (0x01 & debug) \
  328. DUMP(buf_, len_); \
  329. } while (0)
  330. #else
  331. #define DUMP(buf_, len_)
  332. #define DUMP1(buf_, len_)
  333. #endif
  334. /* module parameters */
  335. static int debug;
  336. static int tty_major;
  337. static int disable_net;
  338. /* driver info */
  339. static const char driver_name[] = "hso";
  340. static const char tty_filename[] = "ttyHS";
  341. static const char *version = __FILE__ ": " DRIVER_VERSION " " MOD_AUTHOR;
  342. /* the usb driver itself (registered in hso_init) */
  343. static struct usb_driver hso_driver;
  344. /* serial structures */
  345. static struct tty_driver *tty_drv;
  346. static struct hso_device *serial_table[HSO_SERIAL_TTY_MINORS];
  347. static struct hso_device *network_table[HSO_MAX_NET_DEVICES];
  348. static spinlock_t serial_table_lock;
  349. static const s32 default_port_spec[] = {
  350. HSO_INTF_MUX | HSO_PORT_NETWORK,
  351. HSO_INTF_BULK | HSO_PORT_DIAG,
  352. HSO_INTF_BULK | HSO_PORT_MODEM,
  353. 0
  354. };
  355. static const s32 icon321_port_spec[] = {
  356. HSO_INTF_MUX | HSO_PORT_NETWORK,
  357. HSO_INTF_BULK | HSO_PORT_DIAG2,
  358. HSO_INTF_BULK | HSO_PORT_MODEM,
  359. HSO_INTF_BULK | HSO_PORT_DIAG,
  360. 0
  361. };
  362. #define default_port_device(vendor, product) \
  363. USB_DEVICE(vendor, product), \
  364. .driver_info = (kernel_ulong_t)default_port_spec
  365. #define icon321_port_device(vendor, product) \
  366. USB_DEVICE(vendor, product), \
  367. .driver_info = (kernel_ulong_t)icon321_port_spec
  368. /* list of devices we support */
  369. static const struct usb_device_id hso_ids[] = {
  370. {default_port_device(0x0af0, 0x6711)},
  371. {default_port_device(0x0af0, 0x6731)},
  372. {default_port_device(0x0af0, 0x6751)},
  373. {default_port_device(0x0af0, 0x6771)},
  374. {default_port_device(0x0af0, 0x6791)},
  375. {default_port_device(0x0af0, 0x6811)},
  376. {default_port_device(0x0af0, 0x6911)},
  377. {default_port_device(0x0af0, 0x6951)},
  378. {default_port_device(0x0af0, 0x6971)},
  379. {default_port_device(0x0af0, 0x7011)},
  380. {default_port_device(0x0af0, 0x7031)},
  381. {default_port_device(0x0af0, 0x7051)},
  382. {default_port_device(0x0af0, 0x7071)},
  383. {default_port_device(0x0af0, 0x7111)},
  384. {default_port_device(0x0af0, 0x7211)},
  385. {default_port_device(0x0af0, 0x7251)},
  386. {default_port_device(0x0af0, 0x7271)},
  387. {default_port_device(0x0af0, 0x7311)},
  388. {default_port_device(0x0af0, 0xc031)}, /* Icon-Edge */
  389. {icon321_port_device(0x0af0, 0xd013)}, /* Module HSxPA */
  390. {icon321_port_device(0x0af0, 0xd031)}, /* Icon-321 */
  391. {icon321_port_device(0x0af0, 0xd033)}, /* Icon-322 */
  392. {USB_DEVICE(0x0af0, 0x7301)}, /* GE40x */
  393. {USB_DEVICE(0x0af0, 0x7361)}, /* GE40x */
  394. {USB_DEVICE(0x0af0, 0x7381)}, /* GE40x */
  395. {USB_DEVICE(0x0af0, 0x7401)}, /* GI 0401 */
  396. {USB_DEVICE(0x0af0, 0x7501)}, /* GTM 382 */
  397. {USB_DEVICE(0x0af0, 0x7601)}, /* GE40x */
  398. {USB_DEVICE(0x0af0, 0x7701)},
  399. {USB_DEVICE(0x0af0, 0x7801)},
  400. {USB_DEVICE(0x0af0, 0x7901)},
  401. {USB_DEVICE(0x0af0, 0x7361)},
  402. {USB_DEVICE(0x0af0, 0xd057)},
  403. {USB_DEVICE(0x0af0, 0xd055)},
  404. {}
  405. };
  406. MODULE_DEVICE_TABLE(usb, hso_ids);
  407. /* Sysfs attribute */
  408. static ssize_t hso_sysfs_show_porttype(struct device *dev,
  409. struct device_attribute *attr,
  410. char *buf)
  411. {
  412. struct hso_device *hso_dev = dev->driver_data;
  413. char *port_name;
  414. if (!hso_dev)
  415. return 0;
  416. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  417. case HSO_PORT_CONTROL:
  418. port_name = "Control";
  419. break;
  420. case HSO_PORT_APP:
  421. port_name = "Application";
  422. break;
  423. case HSO_PORT_APP2:
  424. port_name = "Application2";
  425. break;
  426. case HSO_PORT_GPS:
  427. port_name = "GPS";
  428. break;
  429. case HSO_PORT_GPS_CONTROL:
  430. port_name = "GPS Control";
  431. break;
  432. case HSO_PORT_PCSC:
  433. port_name = "PCSC";
  434. break;
  435. case HSO_PORT_DIAG:
  436. port_name = "Diagnostic";
  437. break;
  438. case HSO_PORT_DIAG2:
  439. port_name = "Diagnostic2";
  440. break;
  441. case HSO_PORT_MODEM:
  442. port_name = "Modem";
  443. break;
  444. case HSO_PORT_NETWORK:
  445. port_name = "Network";
  446. break;
  447. default:
  448. port_name = "Unknown";
  449. break;
  450. }
  451. return sprintf(buf, "%s\n", port_name);
  452. }
  453. static DEVICE_ATTR(hsotype, S_IRUGO, hso_sysfs_show_porttype, NULL);
  454. static int hso_urb_to_index(struct hso_serial *serial, struct urb *urb)
  455. {
  456. int idx;
  457. for (idx = 0; idx < serial->num_rx_urbs; idx++)
  458. if (serial->rx_urb[idx] == urb)
  459. return idx;
  460. dev_err(serial->parent->dev, "hso_urb_to_index failed\n");
  461. return -1;
  462. }
  463. /* converts mux value to a port spec value */
  464. static u32 hso_mux_to_port(int mux)
  465. {
  466. u32 result;
  467. switch (mux) {
  468. case 0x1:
  469. result = HSO_PORT_CONTROL;
  470. break;
  471. case 0x2:
  472. result = HSO_PORT_APP;
  473. break;
  474. case 0x4:
  475. result = HSO_PORT_PCSC;
  476. break;
  477. case 0x8:
  478. result = HSO_PORT_GPS;
  479. break;
  480. case 0x10:
  481. result = HSO_PORT_APP2;
  482. break;
  483. default:
  484. result = HSO_PORT_NO_PORT;
  485. }
  486. return result;
  487. }
  488. /* converts port spec value to a mux value */
  489. static u32 hso_port_to_mux(int port)
  490. {
  491. u32 result;
  492. switch (port & HSO_PORT_MASK) {
  493. case HSO_PORT_CONTROL:
  494. result = 0x0;
  495. break;
  496. case HSO_PORT_APP:
  497. result = 0x1;
  498. break;
  499. case HSO_PORT_PCSC:
  500. result = 0x2;
  501. break;
  502. case HSO_PORT_GPS:
  503. result = 0x3;
  504. break;
  505. case HSO_PORT_APP2:
  506. result = 0x4;
  507. break;
  508. default:
  509. result = 0x0;
  510. }
  511. return result;
  512. }
  513. static struct hso_serial *get_serial_by_shared_int_and_type(
  514. struct hso_shared_int *shared_int,
  515. int mux)
  516. {
  517. int i, port;
  518. port = hso_mux_to_port(mux);
  519. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  520. if (serial_table[i]
  521. && (dev2ser(serial_table[i])->shared_int == shared_int)
  522. && ((serial_table[i]->port_spec & HSO_PORT_MASK) == port)) {
  523. return dev2ser(serial_table[i]);
  524. }
  525. }
  526. return NULL;
  527. }
  528. static struct hso_serial *get_serial_by_index(unsigned index)
  529. {
  530. struct hso_serial *serial = NULL;
  531. unsigned long flags;
  532. spin_lock_irqsave(&serial_table_lock, flags);
  533. if (serial_table[index])
  534. serial = dev2ser(serial_table[index]);
  535. spin_unlock_irqrestore(&serial_table_lock, flags);
  536. return serial;
  537. }
  538. static int get_free_serial_index(void)
  539. {
  540. int index;
  541. unsigned long flags;
  542. spin_lock_irqsave(&serial_table_lock, flags);
  543. for (index = 0; index < HSO_SERIAL_TTY_MINORS; index++) {
  544. if (serial_table[index] == NULL) {
  545. spin_unlock_irqrestore(&serial_table_lock, flags);
  546. return index;
  547. }
  548. }
  549. spin_unlock_irqrestore(&serial_table_lock, flags);
  550. printk(KERN_ERR "%s: no free serial devices in table\n", __func__);
  551. return -1;
  552. }
  553. static void set_serial_by_index(unsigned index, struct hso_serial *serial)
  554. {
  555. unsigned long flags;
  556. spin_lock_irqsave(&serial_table_lock, flags);
  557. if (serial)
  558. serial_table[index] = serial->parent;
  559. else
  560. serial_table[index] = NULL;
  561. spin_unlock_irqrestore(&serial_table_lock, flags);
  562. }
  563. /* log a meaningful explanation of an USB status */
  564. static void log_usb_status(int status, const char *function)
  565. {
  566. char *explanation;
  567. switch (status) {
  568. case -ENODEV:
  569. explanation = "no device";
  570. break;
  571. case -ENOENT:
  572. explanation = "endpoint not enabled";
  573. break;
  574. case -EPIPE:
  575. explanation = "endpoint stalled";
  576. break;
  577. case -ENOSPC:
  578. explanation = "not enough bandwidth";
  579. break;
  580. case -ESHUTDOWN:
  581. explanation = "device disabled";
  582. break;
  583. case -EHOSTUNREACH:
  584. explanation = "device suspended";
  585. break;
  586. case -EINVAL:
  587. case -EAGAIN:
  588. case -EFBIG:
  589. case -EMSGSIZE:
  590. explanation = "internal error";
  591. break;
  592. default:
  593. explanation = "unknown status";
  594. break;
  595. }
  596. D1("%s: received USB status - %s (%d)", function, explanation, status);
  597. }
  598. /* Network interface functions */
  599. /* called when net interface is brought up by ifconfig */
  600. static int hso_net_open(struct net_device *net)
  601. {
  602. struct hso_net *odev = netdev_priv(net);
  603. unsigned long flags = 0;
  604. if (!odev) {
  605. dev_err(&net->dev, "No net device !\n");
  606. return -ENODEV;
  607. }
  608. odev->skb_tx_buf = NULL;
  609. /* setup environment */
  610. spin_lock_irqsave(&odev->net_lock, flags);
  611. odev->rx_parse_state = WAIT_IP;
  612. odev->rx_buf_size = 0;
  613. odev->rx_buf_missing = sizeof(struct iphdr);
  614. spin_unlock_irqrestore(&odev->net_lock, flags);
  615. /* We are up and running. */
  616. set_bit(HSO_NET_RUNNING, &odev->flags);
  617. hso_start_net_device(odev->parent);
  618. /* Tell the kernel we are ready to start receiving from it */
  619. netif_start_queue(net);
  620. return 0;
  621. }
  622. /* called when interface is brought down by ifconfig */
  623. static int hso_net_close(struct net_device *net)
  624. {
  625. struct hso_net *odev = netdev_priv(net);
  626. /* we don't need the queue anymore */
  627. netif_stop_queue(net);
  628. /* no longer running */
  629. clear_bit(HSO_NET_RUNNING, &odev->flags);
  630. hso_stop_net_device(odev->parent);
  631. /* done */
  632. return 0;
  633. }
  634. /* USB tells is xmit done, we should start the netqueue again */
  635. static void write_bulk_callback(struct urb *urb)
  636. {
  637. struct hso_net *odev = urb->context;
  638. int status = urb->status;
  639. /* Sanity check */
  640. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  641. dev_err(&urb->dev->dev, "%s: device not running\n", __func__);
  642. return;
  643. }
  644. /* Do we still have a valid kernel network device? */
  645. if (!netif_device_present(odev->net)) {
  646. dev_err(&urb->dev->dev, "%s: net device not present\n",
  647. __func__);
  648. return;
  649. }
  650. /* log status, but don't act on it, we don't need to resubmit anything
  651. * anyhow */
  652. if (status)
  653. log_usb_status(status, __func__);
  654. hso_put_activity(odev->parent);
  655. /* Tell the network interface we are ready for another frame */
  656. netif_wake_queue(odev->net);
  657. }
  658. /* called by kernel when we need to transmit a packet */
  659. static int hso_net_start_xmit(struct sk_buff *skb, struct net_device *net)
  660. {
  661. struct hso_net *odev = netdev_priv(net);
  662. int result;
  663. /* Tell the kernel, "No more frames 'til we are done with this one." */
  664. netif_stop_queue(net);
  665. if (hso_get_activity(odev->parent) == -EAGAIN) {
  666. odev->skb_tx_buf = skb;
  667. return 0;
  668. }
  669. /* log if asked */
  670. DUMP1(skb->data, skb->len);
  671. /* Copy it from kernel memory to OUR memory */
  672. memcpy(odev->mux_bulk_tx_buf, skb->data, skb->len);
  673. D1("len: %d/%d", skb->len, MUX_BULK_TX_BUF_SIZE);
  674. /* Fill in the URB for shipping it out. */
  675. usb_fill_bulk_urb(odev->mux_bulk_tx_urb,
  676. odev->parent->usb,
  677. usb_sndbulkpipe(odev->parent->usb,
  678. odev->out_endp->
  679. bEndpointAddress & 0x7F),
  680. odev->mux_bulk_tx_buf, skb->len, write_bulk_callback,
  681. odev);
  682. /* Deal with the Zero Length packet problem, I hope */
  683. odev->mux_bulk_tx_urb->transfer_flags |= URB_ZERO_PACKET;
  684. /* Send the URB on its merry way. */
  685. result = usb_submit_urb(odev->mux_bulk_tx_urb, GFP_ATOMIC);
  686. if (result) {
  687. dev_warn(&odev->parent->interface->dev,
  688. "failed mux_bulk_tx_urb %d", result);
  689. net->stats.tx_errors++;
  690. netif_start_queue(net);
  691. } else {
  692. net->stats.tx_packets++;
  693. net->stats.tx_bytes += skb->len;
  694. /* And tell the kernel when the last transmit started. */
  695. net->trans_start = jiffies;
  696. }
  697. dev_kfree_skb(skb);
  698. /* we're done */
  699. return result;
  700. }
  701. static void hso_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info)
  702. {
  703. struct hso_net *odev = netdev_priv(net);
  704. strncpy(info->driver, driver_name, ETHTOOL_BUSINFO_LEN);
  705. strncpy(info->version, DRIVER_VERSION, ETHTOOL_BUSINFO_LEN);
  706. usb_make_path(odev->parent->usb, info->bus_info, sizeof info->bus_info);
  707. }
  708. static struct ethtool_ops ops = {
  709. .get_drvinfo = hso_get_drvinfo,
  710. .get_link = ethtool_op_get_link
  711. };
  712. /* called when a packet did not ack after watchdogtimeout */
  713. static void hso_net_tx_timeout(struct net_device *net)
  714. {
  715. struct hso_net *odev = netdev_priv(net);
  716. if (!odev)
  717. return;
  718. /* Tell syslog we are hosed. */
  719. dev_warn(&net->dev, "Tx timed out.\n");
  720. /* Tear the waiting frame off the list */
  721. if (odev->mux_bulk_tx_urb
  722. && (odev->mux_bulk_tx_urb->status == -EINPROGRESS))
  723. usb_unlink_urb(odev->mux_bulk_tx_urb);
  724. /* Update statistics */
  725. net->stats.tx_errors++;
  726. }
  727. /* make a real packet from the received USB buffer */
  728. static void packetizeRx(struct hso_net *odev, unsigned char *ip_pkt,
  729. unsigned int count, unsigned char is_eop)
  730. {
  731. unsigned short temp_bytes;
  732. unsigned short buffer_offset = 0;
  733. unsigned short frame_len;
  734. unsigned char *tmp_rx_buf;
  735. /* log if needed */
  736. D1("Rx %d bytes", count);
  737. DUMP(ip_pkt, min(128, (int)count));
  738. while (count) {
  739. switch (odev->rx_parse_state) {
  740. case WAIT_IP:
  741. /* waiting for IP header. */
  742. /* wanted bytes - size of ip header */
  743. temp_bytes =
  744. (count <
  745. odev->rx_buf_missing) ? count : odev->
  746. rx_buf_missing;
  747. memcpy(((unsigned char *)(&odev->rx_ip_hdr)) +
  748. odev->rx_buf_size, ip_pkt + buffer_offset,
  749. temp_bytes);
  750. odev->rx_buf_size += temp_bytes;
  751. buffer_offset += temp_bytes;
  752. odev->rx_buf_missing -= temp_bytes;
  753. count -= temp_bytes;
  754. if (!odev->rx_buf_missing) {
  755. /* header is complete allocate an sk_buffer and
  756. * continue to WAIT_DATA */
  757. frame_len = ntohs(odev->rx_ip_hdr.tot_len);
  758. if ((frame_len > DEFAULT_MRU) ||
  759. (frame_len < sizeof(struct iphdr))) {
  760. dev_err(&odev->net->dev,
  761. "Invalid frame (%d) length\n",
  762. frame_len);
  763. odev->rx_parse_state = WAIT_SYNC;
  764. continue;
  765. }
  766. /* Allocate an sk_buff */
  767. odev->skb_rx_buf = dev_alloc_skb(frame_len);
  768. if (!odev->skb_rx_buf) {
  769. /* We got no receive buffer. */
  770. D1("could not allocate memory");
  771. odev->rx_parse_state = WAIT_SYNC;
  772. return;
  773. }
  774. /* Here's where it came from */
  775. odev->skb_rx_buf->dev = odev->net;
  776. /* Copy what we got so far. make room for iphdr
  777. * after tail. */
  778. tmp_rx_buf =
  779. skb_put(odev->skb_rx_buf,
  780. sizeof(struct iphdr));
  781. memcpy(tmp_rx_buf, (char *)&(odev->rx_ip_hdr),
  782. sizeof(struct iphdr));
  783. /* ETH_HLEN */
  784. odev->rx_buf_size = sizeof(struct iphdr);
  785. /* Filip actually use .tot_len */
  786. odev->rx_buf_missing =
  787. frame_len - sizeof(struct iphdr);
  788. odev->rx_parse_state = WAIT_DATA;
  789. }
  790. break;
  791. case WAIT_DATA:
  792. temp_bytes = (count < odev->rx_buf_missing)
  793. ? count : odev->rx_buf_missing;
  794. /* Copy the rest of the bytes that are left in the
  795. * buffer into the waiting sk_buf. */
  796. /* Make room for temp_bytes after tail. */
  797. tmp_rx_buf = skb_put(odev->skb_rx_buf, temp_bytes);
  798. memcpy(tmp_rx_buf, ip_pkt + buffer_offset, temp_bytes);
  799. odev->rx_buf_missing -= temp_bytes;
  800. count -= temp_bytes;
  801. buffer_offset += temp_bytes;
  802. odev->rx_buf_size += temp_bytes;
  803. if (!odev->rx_buf_missing) {
  804. /* Packet is complete. Inject into stack. */
  805. /* We have IP packet here */
  806. odev->skb_rx_buf->protocol =
  807. __constant_htons(ETH_P_IP);
  808. /* don't check it */
  809. odev->skb_rx_buf->ip_summed =
  810. CHECKSUM_UNNECESSARY;
  811. skb_reset_mac_header(odev->skb_rx_buf);
  812. /* Ship it off to the kernel */
  813. netif_rx(odev->skb_rx_buf);
  814. /* No longer our buffer. */
  815. odev->skb_rx_buf = NULL;
  816. /* update out statistics */
  817. odev->net->stats.rx_packets++;
  818. odev->net->stats.rx_bytes += odev->rx_buf_size;
  819. odev->rx_buf_size = 0;
  820. odev->rx_buf_missing = sizeof(struct iphdr);
  821. odev->rx_parse_state = WAIT_IP;
  822. }
  823. break;
  824. case WAIT_SYNC:
  825. D1(" W_S");
  826. count = 0;
  827. break;
  828. default:
  829. D1(" ");
  830. count--;
  831. break;
  832. }
  833. }
  834. /* Recovery mechanism for WAIT_SYNC state. */
  835. if (is_eop) {
  836. if (odev->rx_parse_state == WAIT_SYNC) {
  837. odev->rx_parse_state = WAIT_IP;
  838. odev->rx_buf_size = 0;
  839. odev->rx_buf_missing = sizeof(struct iphdr);
  840. }
  841. }
  842. }
  843. /* Moving data from usb to kernel (in interrupt state) */
  844. static void read_bulk_callback(struct urb *urb)
  845. {
  846. struct hso_net *odev = urb->context;
  847. struct net_device *net;
  848. int result;
  849. int status = urb->status;
  850. /* is al ok? (Filip: Who's Al ?) */
  851. if (status) {
  852. log_usb_status(status, __func__);
  853. return;
  854. }
  855. /* Sanity check */
  856. if (!odev || !test_bit(HSO_NET_RUNNING, &odev->flags)) {
  857. D1("BULK IN callback but driver is not active!");
  858. return;
  859. }
  860. usb_mark_last_busy(urb->dev);
  861. net = odev->net;
  862. if (!netif_device_present(net)) {
  863. /* Somebody killed our network interface... */
  864. return;
  865. }
  866. if (odev->parent->port_spec & HSO_INFO_CRC_BUG) {
  867. u32 rest;
  868. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  869. rest = urb->actual_length % odev->in_endp->wMaxPacketSize;
  870. if (((rest == 5) || (rest == 6))
  871. && !memcmp(((u8 *) urb->transfer_buffer) +
  872. urb->actual_length - 4, crc_check, 4)) {
  873. urb->actual_length -= 4;
  874. }
  875. }
  876. /* do we even have a packet? */
  877. if (urb->actual_length) {
  878. /* Handle the IP stream, add header and push it onto network
  879. * stack if the packet is complete. */
  880. spin_lock(&odev->net_lock);
  881. packetizeRx(odev, urb->transfer_buffer, urb->actual_length,
  882. (urb->transfer_buffer_length >
  883. urb->actual_length) ? 1 : 0);
  884. spin_unlock(&odev->net_lock);
  885. }
  886. /* We are done with this URB, resubmit it. Prep the USB to wait for
  887. * another frame. Reuse same as received. */
  888. usb_fill_bulk_urb(urb,
  889. odev->parent->usb,
  890. usb_rcvbulkpipe(odev->parent->usb,
  891. odev->in_endp->
  892. bEndpointAddress & 0x7F),
  893. urb->transfer_buffer, MUX_BULK_RX_BUF_SIZE,
  894. read_bulk_callback, odev);
  895. /* Give this to the USB subsystem so it can tell us when more data
  896. * arrives. */
  897. result = usb_submit_urb(urb, GFP_ATOMIC);
  898. if (result)
  899. dev_warn(&odev->parent->interface->dev,
  900. "%s failed submit mux_bulk_rx_urb %d", __func__,
  901. result);
  902. }
  903. /* Serial driver functions */
  904. static void hso_init_termios(struct ktermios *termios)
  905. {
  906. /*
  907. * The default requirements for this device are:
  908. */
  909. termios->c_iflag &=
  910. ~(IGNBRK /* disable ignore break */
  911. | BRKINT /* disable break causes interrupt */
  912. | PARMRK /* disable mark parity errors */
  913. | ISTRIP /* disable clear high bit of input characters */
  914. | INLCR /* disable translate NL to CR */
  915. | IGNCR /* disable ignore CR */
  916. | ICRNL /* disable translate CR to NL */
  917. | IXON); /* disable enable XON/XOFF flow control */
  918. /* disable postprocess output characters */
  919. termios->c_oflag &= ~OPOST;
  920. termios->c_lflag &=
  921. ~(ECHO /* disable echo input characters */
  922. | ECHONL /* disable echo new line */
  923. | ICANON /* disable erase, kill, werase, and rprnt
  924. special characters */
  925. | ISIG /* disable interrupt, quit, and suspend special
  926. characters */
  927. | IEXTEN); /* disable non-POSIX special characters */
  928. termios->c_cflag &=
  929. ~(CSIZE /* no size */
  930. | PARENB /* disable parity bit */
  931. | CBAUD /* clear current baud rate */
  932. | CBAUDEX); /* clear current buad rate */
  933. termios->c_cflag |= CS8; /* character size 8 bits */
  934. /* baud rate 115200 */
  935. tty_termios_encode_baud_rate(termios, 115200, 115200);
  936. }
  937. static void _hso_serial_set_termios(struct tty_struct *tty,
  938. struct ktermios *old)
  939. {
  940. struct hso_serial *serial = get_serial_by_tty(tty);
  941. struct ktermios *termios;
  942. if (!serial) {
  943. printk(KERN_ERR "%s: no tty structures", __func__);
  944. return;
  945. }
  946. D4("port %d", serial->minor);
  947. /*
  948. * Fix up unsupported bits
  949. */
  950. termios = tty->termios;
  951. termios->c_iflag &= ~IXON; /* disable enable XON/XOFF flow control */
  952. termios->c_cflag &=
  953. ~(CSIZE /* no size */
  954. | PARENB /* disable parity bit */
  955. | CBAUD /* clear current baud rate */
  956. | CBAUDEX); /* clear current buad rate */
  957. termios->c_cflag |= CS8; /* character size 8 bits */
  958. /* baud rate 115200 */
  959. tty_encode_baud_rate(tty, 115200, 115200);
  960. }
  961. static void hso_resubmit_rx_bulk_urb(struct hso_serial *serial, struct urb *urb)
  962. {
  963. int result;
  964. #ifdef CONFIG_HSO_AUTOPM
  965. usb_mark_last_busy(urb->dev);
  966. #endif
  967. /* We are done with this URB, resubmit it. Prep the USB to wait for
  968. * another frame */
  969. usb_fill_bulk_urb(urb, serial->parent->usb,
  970. usb_rcvbulkpipe(serial->parent->usb,
  971. serial->in_endp->
  972. bEndpointAddress & 0x7F),
  973. urb->transfer_buffer, serial->rx_data_length,
  974. hso_std_serial_read_bulk_callback, serial);
  975. /* Give this to the USB subsystem so it can tell us when more data
  976. * arrives. */
  977. result = usb_submit_urb(urb, GFP_ATOMIC);
  978. if (result) {
  979. dev_err(&urb->dev->dev, "%s failed submit serial rx_urb %d\n",
  980. __func__, result);
  981. }
  982. }
  983. static void put_rxbuf_data_and_resubmit_bulk_urb(struct hso_serial *serial)
  984. {
  985. int count;
  986. struct urb *curr_urb;
  987. while (serial->rx_urb_filled[serial->curr_rx_urb_idx]) {
  988. curr_urb = serial->rx_urb[serial->curr_rx_urb_idx];
  989. count = put_rxbuf_data(curr_urb, serial);
  990. if (count == -1)
  991. return;
  992. if (count == 0) {
  993. serial->curr_rx_urb_idx++;
  994. if (serial->curr_rx_urb_idx >= serial->num_rx_urbs)
  995. serial->curr_rx_urb_idx = 0;
  996. hso_resubmit_rx_bulk_urb(serial, curr_urb);
  997. }
  998. }
  999. }
  1000. static void put_rxbuf_data_and_resubmit_ctrl_urb(struct hso_serial *serial)
  1001. {
  1002. int count = 0;
  1003. struct urb *urb;
  1004. urb = serial->rx_urb[0];
  1005. if (serial->open_count > 0) {
  1006. count = put_rxbuf_data(urb, serial);
  1007. if (count == -1)
  1008. return;
  1009. }
  1010. /* Re issue a read as long as we receive data. */
  1011. if (count == 0 && ((urb->actual_length != 0) ||
  1012. (serial->rx_state == RX_PENDING))) {
  1013. serial->rx_state = RX_SENT;
  1014. hso_mux_serial_read(serial);
  1015. } else
  1016. serial->rx_state = RX_IDLE;
  1017. }
  1018. /* read callback for Diag and CS port */
  1019. static void hso_std_serial_read_bulk_callback(struct urb *urb)
  1020. {
  1021. struct hso_serial *serial = urb->context;
  1022. int status = urb->status;
  1023. /* sanity check */
  1024. if (!serial) {
  1025. D1("serial == NULL");
  1026. return;
  1027. } else if (status) {
  1028. log_usb_status(status, __func__);
  1029. return;
  1030. }
  1031. D4("\n--- Got serial_read_bulk callback %02x ---", status);
  1032. D1("Actual length = %d\n", urb->actual_length);
  1033. DUMP1(urb->transfer_buffer, urb->actual_length);
  1034. /* Anyone listening? */
  1035. if (serial->open_count == 0)
  1036. return;
  1037. if (status == 0) {
  1038. if (serial->parent->port_spec & HSO_INFO_CRC_BUG) {
  1039. u32 rest;
  1040. u8 crc_check[4] = { 0xDE, 0xAD, 0xBE, 0xEF };
  1041. rest =
  1042. urb->actual_length %
  1043. serial->in_endp->wMaxPacketSize;
  1044. if (((rest == 5) || (rest == 6))
  1045. && !memcmp(((u8 *) urb->transfer_buffer) +
  1046. urb->actual_length - 4, crc_check, 4)) {
  1047. urb->actual_length -= 4;
  1048. }
  1049. }
  1050. /* Valid data, handle RX data */
  1051. spin_lock(&serial->serial_lock);
  1052. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 1;
  1053. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1054. spin_unlock(&serial->serial_lock);
  1055. } else if (status == -ENOENT || status == -ECONNRESET) {
  1056. /* Unlinked - check for throttled port. */
  1057. D2("Port %d, successfully unlinked urb", serial->minor);
  1058. spin_lock(&serial->serial_lock);
  1059. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1060. hso_resubmit_rx_bulk_urb(serial, urb);
  1061. spin_unlock(&serial->serial_lock);
  1062. } else {
  1063. D2("Port %d, status = %d for read urb", serial->minor, status);
  1064. return;
  1065. }
  1066. }
  1067. /*
  1068. * This needs to be a tasklet otherwise we will
  1069. * end up recursively calling this function.
  1070. */
  1071. void hso_unthrottle_tasklet(struct hso_serial *serial)
  1072. {
  1073. unsigned long flags;
  1074. spin_lock_irqsave(&serial->serial_lock, flags);
  1075. if ((serial->parent->port_spec & HSO_INTF_MUX))
  1076. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1077. else
  1078. put_rxbuf_data_and_resubmit_bulk_urb(serial);
  1079. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1080. }
  1081. static void hso_unthrottle(struct tty_struct *tty)
  1082. {
  1083. struct hso_serial *serial = get_serial_by_tty(tty);
  1084. tasklet_hi_schedule(&serial->unthrottle_tasklet);
  1085. }
  1086. void hso_unthrottle_workfunc(struct work_struct *work)
  1087. {
  1088. struct hso_serial *serial =
  1089. container_of(work, struct hso_serial,
  1090. retry_unthrottle_workqueue);
  1091. hso_unthrottle_tasklet(serial);
  1092. }
  1093. /* open the requested serial port */
  1094. static int hso_serial_open(struct tty_struct *tty, struct file *filp)
  1095. {
  1096. struct hso_serial *serial = get_serial_by_index(tty->index);
  1097. int result;
  1098. /* sanity check */
  1099. if (serial == NULL || serial->magic != HSO_SERIAL_MAGIC) {
  1100. WARN_ON(1);
  1101. tty->driver_data = NULL;
  1102. D1("Failed to open port");
  1103. return -ENODEV;
  1104. }
  1105. mutex_lock(&serial->parent->mutex);
  1106. result = usb_autopm_get_interface(serial->parent->interface);
  1107. if (result < 0)
  1108. goto err_out;
  1109. D1("Opening %d", serial->minor);
  1110. kref_get(&serial->parent->ref);
  1111. /* setup */
  1112. spin_lock_irq(&serial->serial_lock);
  1113. tty->driver_data = serial;
  1114. tty_kref_put(serial->tty);
  1115. serial->tty = tty_kref_get(tty);
  1116. spin_unlock_irq(&serial->serial_lock);
  1117. /* check for port already opened, if not set the termios */
  1118. serial->open_count++;
  1119. if (serial->open_count == 1) {
  1120. tty->low_latency = 1;
  1121. serial->rx_state = RX_IDLE;
  1122. /* Force default termio settings */
  1123. _hso_serial_set_termios(tty, NULL);
  1124. tasklet_init(&serial->unthrottle_tasklet,
  1125. (void (*)(unsigned long))hso_unthrottle_tasklet,
  1126. (unsigned long)serial);
  1127. INIT_WORK(&serial->retry_unthrottle_workqueue,
  1128. hso_unthrottle_workfunc);
  1129. result = hso_start_serial_device(serial->parent, GFP_KERNEL);
  1130. if (result) {
  1131. hso_stop_serial_device(serial->parent);
  1132. serial->open_count--;
  1133. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1134. }
  1135. } else {
  1136. D1("Port was already open");
  1137. }
  1138. usb_autopm_put_interface(serial->parent->interface);
  1139. /* done */
  1140. if (result)
  1141. hso_serial_tiocmset(tty, NULL, TIOCM_RTS | TIOCM_DTR, 0);
  1142. err_out:
  1143. mutex_unlock(&serial->parent->mutex);
  1144. return result;
  1145. }
  1146. /* close the requested serial port */
  1147. static void hso_serial_close(struct tty_struct *tty, struct file *filp)
  1148. {
  1149. struct hso_serial *serial = tty->driver_data;
  1150. u8 usb_gone;
  1151. D1("Closing serial port");
  1152. /* Open failed, no close cleanup required */
  1153. if (serial == NULL)
  1154. return;
  1155. mutex_lock(&serial->parent->mutex);
  1156. usb_gone = serial->parent->usb_gone;
  1157. if (!usb_gone)
  1158. usb_autopm_get_interface(serial->parent->interface);
  1159. /* reset the rts and dtr */
  1160. /* do the actual close */
  1161. serial->open_count--;
  1162. kref_put(&serial->parent->ref, hso_serial_ref_free);
  1163. if (serial->open_count <= 0) {
  1164. serial->open_count = 0;
  1165. spin_lock_irq(&serial->serial_lock);
  1166. if (serial->tty == tty) {
  1167. serial->tty->driver_data = NULL;
  1168. serial->tty = NULL;
  1169. tty_kref_put(tty);
  1170. }
  1171. spin_unlock_irq(&serial->serial_lock);
  1172. if (!usb_gone)
  1173. hso_stop_serial_device(serial->parent);
  1174. tasklet_kill(&serial->unthrottle_tasklet);
  1175. cancel_work_sync(&serial->retry_unthrottle_workqueue);
  1176. }
  1177. if (!usb_gone)
  1178. usb_autopm_put_interface(serial->parent->interface);
  1179. mutex_unlock(&serial->parent->mutex);
  1180. }
  1181. /* close the requested serial port */
  1182. static int hso_serial_write(struct tty_struct *tty, const unsigned char *buf,
  1183. int count)
  1184. {
  1185. struct hso_serial *serial = get_serial_by_tty(tty);
  1186. int space, tx_bytes;
  1187. unsigned long flags;
  1188. /* sanity check */
  1189. if (serial == NULL) {
  1190. printk(KERN_ERR "%s: serial is NULL\n", __func__);
  1191. return -ENODEV;
  1192. }
  1193. spin_lock_irqsave(&serial->serial_lock, flags);
  1194. space = serial->tx_data_length - serial->tx_buffer_count;
  1195. tx_bytes = (count < space) ? count : space;
  1196. if (!tx_bytes)
  1197. goto out;
  1198. memcpy(serial->tx_buffer + serial->tx_buffer_count, buf, tx_bytes);
  1199. serial->tx_buffer_count += tx_bytes;
  1200. out:
  1201. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1202. hso_kick_transmit(serial);
  1203. /* done */
  1204. return tx_bytes;
  1205. }
  1206. /* how much room is there for writing */
  1207. static int hso_serial_write_room(struct tty_struct *tty)
  1208. {
  1209. struct hso_serial *serial = get_serial_by_tty(tty);
  1210. int room;
  1211. unsigned long flags;
  1212. spin_lock_irqsave(&serial->serial_lock, flags);
  1213. room = serial->tx_data_length - serial->tx_buffer_count;
  1214. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1215. /* return free room */
  1216. return room;
  1217. }
  1218. /* setup the term */
  1219. static void hso_serial_set_termios(struct tty_struct *tty, struct ktermios *old)
  1220. {
  1221. struct hso_serial *serial = get_serial_by_tty(tty);
  1222. unsigned long flags;
  1223. if (old)
  1224. D5("Termios called with: cflags new[%d] - old[%d]",
  1225. tty->termios->c_cflag, old->c_cflag);
  1226. /* the actual setup */
  1227. spin_lock_irqsave(&serial->serial_lock, flags);
  1228. if (serial->open_count)
  1229. _hso_serial_set_termios(tty, old);
  1230. else
  1231. tty->termios = old;
  1232. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1233. /* done */
  1234. return;
  1235. }
  1236. /* how many characters in the buffer */
  1237. static int hso_serial_chars_in_buffer(struct tty_struct *tty)
  1238. {
  1239. struct hso_serial *serial = get_serial_by_tty(tty);
  1240. int chars;
  1241. unsigned long flags;
  1242. /* sanity check */
  1243. if (serial == NULL)
  1244. return 0;
  1245. spin_lock_irqsave(&serial->serial_lock, flags);
  1246. chars = serial->tx_buffer_count;
  1247. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1248. return chars;
  1249. }
  1250. int tiocmget_submit_urb(struct hso_serial *serial,
  1251. struct hso_tiocmget *tiocmget,
  1252. struct usb_device *usb)
  1253. {
  1254. int result;
  1255. if (serial->parent->usb_gone)
  1256. return -ENODEV;
  1257. usb_fill_int_urb(tiocmget->urb, usb,
  1258. usb_rcvintpipe(usb,
  1259. tiocmget->endp->
  1260. bEndpointAddress & 0x7F),
  1261. &tiocmget->serial_state_notification,
  1262. sizeof(struct hso_serial_state_notification),
  1263. tiocmget_intr_callback, serial,
  1264. tiocmget->endp->bInterval);
  1265. result = usb_submit_urb(tiocmget->urb, GFP_ATOMIC);
  1266. if (result) {
  1267. dev_warn(&usb->dev, "%s usb_submit_urb failed %d\n", __func__,
  1268. result);
  1269. }
  1270. return result;
  1271. }
  1272. static void tiocmget_intr_callback(struct urb *urb)
  1273. {
  1274. struct hso_serial *serial = urb->context;
  1275. struct hso_tiocmget *tiocmget;
  1276. int status = urb->status;
  1277. u16 UART_state_bitmap, prev_UART_state_bitmap;
  1278. struct uart_icount *icount;
  1279. struct hso_serial_state_notification *serial_state_notification;
  1280. struct usb_device *usb;
  1281. /* Sanity checks */
  1282. if (!serial)
  1283. return;
  1284. if (status) {
  1285. log_usb_status(status, __func__);
  1286. return;
  1287. }
  1288. tiocmget = serial->tiocmget;
  1289. if (!tiocmget)
  1290. return;
  1291. usb = serial->parent->usb;
  1292. serial_state_notification = &tiocmget->serial_state_notification;
  1293. if (serial_state_notification->bmRequestType != BM_REQUEST_TYPE ||
  1294. serial_state_notification->bNotification != B_NOTIFICATION ||
  1295. le16_to_cpu(serial_state_notification->wValue) != W_VALUE ||
  1296. le16_to_cpu(serial_state_notification->wIndex) != W_INDEX ||
  1297. le16_to_cpu(serial_state_notification->wLength) != W_LENGTH) {
  1298. dev_warn(&usb->dev,
  1299. "hso received invalid serial state notification\n");
  1300. DUMP(serial_state_notification,
  1301. sizeof(hso_serial_state_notifation))
  1302. } else {
  1303. UART_state_bitmap = le16_to_cpu(serial_state_notification->
  1304. UART_state_bitmap);
  1305. prev_UART_state_bitmap = tiocmget->prev_UART_state_bitmap;
  1306. icount = &tiocmget->icount;
  1307. spin_lock(&serial->serial_lock);
  1308. if ((UART_state_bitmap & B_OVERRUN) !=
  1309. (prev_UART_state_bitmap & B_OVERRUN))
  1310. icount->parity++;
  1311. if ((UART_state_bitmap & B_PARITY) !=
  1312. (prev_UART_state_bitmap & B_PARITY))
  1313. icount->parity++;
  1314. if ((UART_state_bitmap & B_FRAMING) !=
  1315. (prev_UART_state_bitmap & B_FRAMING))
  1316. icount->frame++;
  1317. if ((UART_state_bitmap & B_RING_SIGNAL) &&
  1318. !(prev_UART_state_bitmap & B_RING_SIGNAL))
  1319. icount->rng++;
  1320. if ((UART_state_bitmap & B_BREAK) !=
  1321. (prev_UART_state_bitmap & B_BREAK))
  1322. icount->brk++;
  1323. if ((UART_state_bitmap & B_TX_CARRIER) !=
  1324. (prev_UART_state_bitmap & B_TX_CARRIER))
  1325. icount->dsr++;
  1326. if ((UART_state_bitmap & B_RX_CARRIER) !=
  1327. (prev_UART_state_bitmap & B_RX_CARRIER))
  1328. icount->dcd++;
  1329. tiocmget->prev_UART_state_bitmap = UART_state_bitmap;
  1330. spin_unlock(&serial->serial_lock);
  1331. tiocmget->intr_completed = 1;
  1332. wake_up_interruptible(&tiocmget->waitq);
  1333. }
  1334. memset(serial_state_notification, 0,
  1335. sizeof(struct hso_serial_state_notification));
  1336. tiocmget_submit_urb(serial,
  1337. tiocmget,
  1338. serial->parent->usb);
  1339. }
  1340. /*
  1341. * next few functions largely stolen from drivers/serial/serial_core.c
  1342. */
  1343. /* Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1344. * - mask passed in arg for lines of interest
  1345. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1346. * Caller should use TIOCGICOUNT to see which one it was
  1347. */
  1348. static int
  1349. hso_wait_modem_status(struct hso_serial *serial, unsigned long arg)
  1350. {
  1351. DECLARE_WAITQUEUE(wait, current);
  1352. struct uart_icount cprev, cnow;
  1353. struct hso_tiocmget *tiocmget;
  1354. int ret;
  1355. tiocmget = serial->tiocmget;
  1356. if (!tiocmget)
  1357. return -ENOENT;
  1358. /*
  1359. * note the counters on entry
  1360. */
  1361. spin_lock_irq(&serial->serial_lock);
  1362. memcpy(&cprev, &tiocmget->icount, sizeof(struct uart_icount));
  1363. spin_unlock_irq(&serial->serial_lock);
  1364. add_wait_queue(&tiocmget->waitq, &wait);
  1365. for (;;) {
  1366. spin_lock_irq(&serial->serial_lock);
  1367. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1368. spin_unlock_irq(&serial->serial_lock);
  1369. set_current_state(TASK_INTERRUPTIBLE);
  1370. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1371. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1372. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd))) {
  1373. ret = 0;
  1374. break;
  1375. }
  1376. schedule();
  1377. /* see if a signal did it */
  1378. if (signal_pending(current)) {
  1379. ret = -ERESTARTSYS;
  1380. break;
  1381. }
  1382. cprev = cnow;
  1383. }
  1384. current->state = TASK_RUNNING;
  1385. remove_wait_queue(&tiocmget->waitq, &wait);
  1386. return ret;
  1387. }
  1388. /*
  1389. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1390. * Return: write counters to the user passed counter struct
  1391. * NB: both 1->0 and 0->1 transitions are counted except for
  1392. * RI where only 0->1 is counted.
  1393. */
  1394. static int hso_get_count(struct hso_serial *serial,
  1395. struct serial_icounter_struct __user *icnt)
  1396. {
  1397. struct serial_icounter_struct icount;
  1398. struct uart_icount cnow;
  1399. struct hso_tiocmget *tiocmget = serial->tiocmget;
  1400. if (!tiocmget)
  1401. return -ENOENT;
  1402. spin_lock_irq(&serial->serial_lock);
  1403. memcpy(&cnow, &tiocmget->icount, sizeof(struct uart_icount));
  1404. spin_unlock_irq(&serial->serial_lock);
  1405. icount.cts = cnow.cts;
  1406. icount.dsr = cnow.dsr;
  1407. icount.rng = cnow.rng;
  1408. icount.dcd = cnow.dcd;
  1409. icount.rx = cnow.rx;
  1410. icount.tx = cnow.tx;
  1411. icount.frame = cnow.frame;
  1412. icount.overrun = cnow.overrun;
  1413. icount.parity = cnow.parity;
  1414. icount.brk = cnow.brk;
  1415. icount.buf_overrun = cnow.buf_overrun;
  1416. return copy_to_user(icnt, &icount, sizeof(icount)) ? -EFAULT : 0;
  1417. }
  1418. static int hso_serial_tiocmget(struct tty_struct *tty, struct file *file)
  1419. {
  1420. int retval;
  1421. struct hso_serial *serial = get_serial_by_tty(tty);
  1422. struct hso_tiocmget *tiocmget;
  1423. u16 UART_state_bitmap;
  1424. /* sanity check */
  1425. if (!serial) {
  1426. D1("no tty structures");
  1427. return -EINVAL;
  1428. }
  1429. spin_lock_irq(&serial->serial_lock);
  1430. retval = ((serial->rts_state) ? TIOCM_RTS : 0) |
  1431. ((serial->dtr_state) ? TIOCM_DTR : 0);
  1432. tiocmget = serial->tiocmget;
  1433. if (tiocmget) {
  1434. UART_state_bitmap = le16_to_cpu(
  1435. tiocmget->prev_UART_state_bitmap);
  1436. if (UART_state_bitmap & B_RING_SIGNAL)
  1437. retval |= TIOCM_RNG;
  1438. if (UART_state_bitmap & B_RX_CARRIER)
  1439. retval |= TIOCM_CD;
  1440. if (UART_state_bitmap & B_TX_CARRIER)
  1441. retval |= TIOCM_DSR;
  1442. }
  1443. spin_unlock_irq(&serial->serial_lock);
  1444. return retval;
  1445. }
  1446. static int hso_serial_tiocmset(struct tty_struct *tty, struct file *file,
  1447. unsigned int set, unsigned int clear)
  1448. {
  1449. int val = 0;
  1450. unsigned long flags;
  1451. int if_num;
  1452. struct hso_serial *serial = get_serial_by_tty(tty);
  1453. /* sanity check */
  1454. if (!serial) {
  1455. D1("no tty structures");
  1456. return -EINVAL;
  1457. }
  1458. if_num = serial->parent->interface->altsetting->desc.bInterfaceNumber;
  1459. spin_lock_irqsave(&serial->serial_lock, flags);
  1460. if (set & TIOCM_RTS)
  1461. serial->rts_state = 1;
  1462. if (set & TIOCM_DTR)
  1463. serial->dtr_state = 1;
  1464. if (clear & TIOCM_RTS)
  1465. serial->rts_state = 0;
  1466. if (clear & TIOCM_DTR)
  1467. serial->dtr_state = 0;
  1468. if (serial->dtr_state)
  1469. val |= 0x01;
  1470. if (serial->rts_state)
  1471. val |= 0x02;
  1472. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1473. return usb_control_msg(serial->parent->usb,
  1474. usb_rcvctrlpipe(serial->parent->usb, 0), 0x22,
  1475. 0x21, val, if_num, NULL, 0,
  1476. USB_CTRL_SET_TIMEOUT);
  1477. }
  1478. static int hso_serial_ioctl(struct tty_struct *tty, struct file *file,
  1479. unsigned int cmd, unsigned long arg)
  1480. {
  1481. struct hso_serial *serial = get_serial_by_tty(tty);
  1482. void __user *uarg = (void __user *)arg;
  1483. int ret = 0;
  1484. D4("IOCTL cmd: %d, arg: %ld", cmd, arg);
  1485. if (!serial)
  1486. return -ENODEV;
  1487. switch (cmd) {
  1488. case TIOCMIWAIT:
  1489. ret = hso_wait_modem_status(serial, arg);
  1490. break;
  1491. case TIOCGICOUNT:
  1492. ret = hso_get_count(serial, uarg);
  1493. break;
  1494. default:
  1495. ret = -ENOIOCTLCMD;
  1496. break;
  1497. }
  1498. return ret;
  1499. }
  1500. /* starts a transmit */
  1501. static void hso_kick_transmit(struct hso_serial *serial)
  1502. {
  1503. u8 *temp;
  1504. unsigned long flags;
  1505. int res;
  1506. spin_lock_irqsave(&serial->serial_lock, flags);
  1507. if (!serial->tx_buffer_count)
  1508. goto out;
  1509. if (serial->tx_urb_used)
  1510. goto out;
  1511. /* Wakeup USB interface if necessary */
  1512. if (hso_get_activity(serial->parent) == -EAGAIN)
  1513. goto out;
  1514. /* Switch pointers around to avoid memcpy */
  1515. temp = serial->tx_buffer;
  1516. serial->tx_buffer = serial->tx_data;
  1517. serial->tx_data = temp;
  1518. serial->tx_data_count = serial->tx_buffer_count;
  1519. serial->tx_buffer_count = 0;
  1520. /* If temp is set, it means we switched buffers */
  1521. if (temp && serial->write_data) {
  1522. res = serial->write_data(serial);
  1523. if (res >= 0)
  1524. serial->tx_urb_used = 1;
  1525. }
  1526. out:
  1527. spin_unlock_irqrestore(&serial->serial_lock, flags);
  1528. }
  1529. /* make a request (for reading and writing data to muxed serial port) */
  1530. static int mux_device_request(struct hso_serial *serial, u8 type, u16 port,
  1531. struct urb *ctrl_urb,
  1532. struct usb_ctrlrequest *ctrl_req,
  1533. u8 *ctrl_urb_data, u32 size)
  1534. {
  1535. int result;
  1536. int pipe;
  1537. /* Sanity check */
  1538. if (!serial || !ctrl_urb || !ctrl_req) {
  1539. printk(KERN_ERR "%s: Wrong arguments\n", __func__);
  1540. return -EINVAL;
  1541. }
  1542. /* initialize */
  1543. ctrl_req->wValue = 0;
  1544. ctrl_req->wIndex = cpu_to_le16(hso_port_to_mux(port));
  1545. ctrl_req->wLength = cpu_to_le16(size);
  1546. if (type == USB_CDC_GET_ENCAPSULATED_RESPONSE) {
  1547. /* Reading command */
  1548. ctrl_req->bRequestType = USB_DIR_IN |
  1549. USB_TYPE_OPTION_VENDOR |
  1550. USB_RECIP_INTERFACE;
  1551. ctrl_req->bRequest = USB_CDC_GET_ENCAPSULATED_RESPONSE;
  1552. pipe = usb_rcvctrlpipe(serial->parent->usb, 0);
  1553. } else {
  1554. /* Writing command */
  1555. ctrl_req->bRequestType = USB_DIR_OUT |
  1556. USB_TYPE_OPTION_VENDOR |
  1557. USB_RECIP_INTERFACE;
  1558. ctrl_req->bRequest = USB_CDC_SEND_ENCAPSULATED_COMMAND;
  1559. pipe = usb_sndctrlpipe(serial->parent->usb, 0);
  1560. }
  1561. /* syslog */
  1562. D2("%s command (%02x) len: %d, port: %d",
  1563. type == USB_CDC_GET_ENCAPSULATED_RESPONSE ? "Read" : "Write",
  1564. ctrl_req->bRequestType, ctrl_req->wLength, port);
  1565. /* Load ctrl urb */
  1566. ctrl_urb->transfer_flags = 0;
  1567. usb_fill_control_urb(ctrl_urb,
  1568. serial->parent->usb,
  1569. pipe,
  1570. (u8 *) ctrl_req,
  1571. ctrl_urb_data, size, ctrl_callback, serial);
  1572. /* Send it on merry way */
  1573. result = usb_submit_urb(ctrl_urb, GFP_ATOMIC);
  1574. if (result) {
  1575. dev_err(&ctrl_urb->dev->dev,
  1576. "%s failed submit ctrl_urb %d type %d", __func__,
  1577. result, type);
  1578. return result;
  1579. }
  1580. /* done */
  1581. return size;
  1582. }
  1583. /* called by intr_callback when read occurs */
  1584. static int hso_mux_serial_read(struct hso_serial *serial)
  1585. {
  1586. if (!serial)
  1587. return -EINVAL;
  1588. /* clean data */
  1589. memset(serial->rx_data[0], 0, CTRL_URB_RX_SIZE);
  1590. /* make the request */
  1591. if (serial->num_rx_urbs != 1) {
  1592. dev_err(&serial->parent->interface->dev,
  1593. "ERROR: mux'd reads with multiple buffers "
  1594. "not possible\n");
  1595. return 0;
  1596. }
  1597. return mux_device_request(serial,
  1598. USB_CDC_GET_ENCAPSULATED_RESPONSE,
  1599. serial->parent->port_spec & HSO_PORT_MASK,
  1600. serial->rx_urb[0],
  1601. &serial->ctrl_req_rx,
  1602. serial->rx_data[0], serial->rx_data_length);
  1603. }
  1604. /* used for muxed serial port callback (muxed serial read) */
  1605. static void intr_callback(struct urb *urb)
  1606. {
  1607. struct hso_shared_int *shared_int = urb->context;
  1608. struct hso_serial *serial;
  1609. unsigned char *port_req;
  1610. int status = urb->status;
  1611. int i;
  1612. usb_mark_last_busy(urb->dev);
  1613. /* sanity check */
  1614. if (!shared_int)
  1615. return;
  1616. /* status check */
  1617. if (status) {
  1618. log_usb_status(status, __func__);
  1619. return;
  1620. }
  1621. D4("\n--- Got intr callback 0x%02X ---", status);
  1622. /* what request? */
  1623. port_req = urb->transfer_buffer;
  1624. D4(" port_req = 0x%.2X\n", *port_req);
  1625. /* loop over all muxed ports to find the one sending this */
  1626. for (i = 0; i < 8; i++) {
  1627. /* max 8 channels on MUX */
  1628. if (*port_req & (1 << i)) {
  1629. serial = get_serial_by_shared_int_and_type(shared_int,
  1630. (1 << i));
  1631. if (serial != NULL) {
  1632. D1("Pending read interrupt on port %d\n", i);
  1633. spin_lock(&serial->serial_lock);
  1634. if (serial->rx_state == RX_IDLE) {
  1635. /* Setup and send a ctrl req read on
  1636. * port i */
  1637. if (!serial->rx_urb_filled[0]) {
  1638. serial->rx_state = RX_SENT;
  1639. hso_mux_serial_read(serial);
  1640. } else
  1641. serial->rx_state = RX_PENDING;
  1642. } else {
  1643. D1("Already pending a read on "
  1644. "port %d\n", i);
  1645. }
  1646. spin_unlock(&serial->serial_lock);
  1647. }
  1648. }
  1649. }
  1650. /* Resubmit interrupt urb */
  1651. hso_mux_submit_intr_urb(shared_int, urb->dev, GFP_ATOMIC);
  1652. }
  1653. /* called for writing to muxed serial port */
  1654. static int hso_mux_serial_write_data(struct hso_serial *serial)
  1655. {
  1656. if (NULL == serial)
  1657. return -EINVAL;
  1658. return mux_device_request(serial,
  1659. USB_CDC_SEND_ENCAPSULATED_COMMAND,
  1660. serial->parent->port_spec & HSO_PORT_MASK,
  1661. serial->tx_urb,
  1662. &serial->ctrl_req_tx,
  1663. serial->tx_data, serial->tx_data_count);
  1664. }
  1665. /* write callback for Diag and CS port */
  1666. static void hso_std_serial_write_bulk_callback(struct urb *urb)
  1667. {
  1668. struct hso_serial *serial = urb->context;
  1669. int status = urb->status;
  1670. struct tty_struct *tty;
  1671. /* sanity check */
  1672. if (!serial) {
  1673. D1("serial == NULL");
  1674. return;
  1675. }
  1676. spin_lock(&serial->serial_lock);
  1677. serial->tx_urb_used = 0;
  1678. tty = tty_kref_get(serial->tty);
  1679. spin_unlock(&serial->serial_lock);
  1680. if (status) {
  1681. log_usb_status(status, __func__);
  1682. tty_kref_put(tty);
  1683. return;
  1684. }
  1685. hso_put_activity(serial->parent);
  1686. if (tty) {
  1687. tty_wakeup(tty);
  1688. tty_kref_put(tty);
  1689. }
  1690. hso_kick_transmit(serial);
  1691. D1(" ");
  1692. return;
  1693. }
  1694. /* called for writing diag or CS serial port */
  1695. static int hso_std_serial_write_data(struct hso_serial *serial)
  1696. {
  1697. int count = serial->tx_data_count;
  1698. int result;
  1699. usb_fill_bulk_urb(serial->tx_urb,
  1700. serial->parent->usb,
  1701. usb_sndbulkpipe(serial->parent->usb,
  1702. serial->out_endp->
  1703. bEndpointAddress & 0x7F),
  1704. serial->tx_data, serial->tx_data_count,
  1705. hso_std_serial_write_bulk_callback, serial);
  1706. result = usb_submit_urb(serial->tx_urb, GFP_ATOMIC);
  1707. if (result) {
  1708. dev_warn(&serial->parent->usb->dev,
  1709. "Failed to submit urb - res %d\n", result);
  1710. return result;
  1711. }
  1712. return count;
  1713. }
  1714. /* callback after read or write on muxed serial port */
  1715. static void ctrl_callback(struct urb *urb)
  1716. {
  1717. struct hso_serial *serial = urb->context;
  1718. struct usb_ctrlrequest *req;
  1719. int status = urb->status;
  1720. struct tty_struct *tty;
  1721. /* sanity check */
  1722. if (!serial)
  1723. return;
  1724. spin_lock(&serial->serial_lock);
  1725. serial->tx_urb_used = 0;
  1726. tty = tty_kref_get(serial->tty);
  1727. spin_unlock(&serial->serial_lock);
  1728. if (status) {
  1729. log_usb_status(status, __func__);
  1730. tty_kref_put(tty);
  1731. return;
  1732. }
  1733. /* what request? */
  1734. req = (struct usb_ctrlrequest *)(urb->setup_packet);
  1735. D4("\n--- Got muxed ctrl callback 0x%02X ---", status);
  1736. D4("Actual length of urb = %d\n", urb->actual_length);
  1737. DUMP1(urb->transfer_buffer, urb->actual_length);
  1738. if (req->bRequestType ==
  1739. (USB_DIR_IN | USB_TYPE_OPTION_VENDOR | USB_RECIP_INTERFACE)) {
  1740. /* response to a read command */
  1741. serial->rx_urb_filled[0] = 1;
  1742. spin_lock(&serial->serial_lock);
  1743. put_rxbuf_data_and_resubmit_ctrl_urb(serial);
  1744. spin_unlock(&serial->serial_lock);
  1745. } else {
  1746. hso_put_activity(serial->parent);
  1747. if (tty)
  1748. tty_wakeup(tty);
  1749. /* response to a write command */
  1750. hso_kick_transmit(serial);
  1751. }
  1752. tty_kref_put(tty);
  1753. }
  1754. /* handle RX data for serial port */
  1755. static int put_rxbuf_data(struct urb *urb, struct hso_serial *serial)
  1756. {
  1757. struct tty_struct *tty;
  1758. int write_length_remaining = 0;
  1759. int curr_write_len;
  1760. /* Sanity check */
  1761. if (urb == NULL || serial == NULL) {
  1762. D1("serial = NULL");
  1763. return -2;
  1764. }
  1765. /* All callers to put_rxbuf_data hold serial_lock */
  1766. tty = tty_kref_get(serial->tty);
  1767. /* Push data to tty */
  1768. if (tty) {
  1769. write_length_remaining = urb->actual_length -
  1770. serial->curr_rx_urb_offset;
  1771. D1("data to push to tty");
  1772. while (write_length_remaining) {
  1773. if (test_bit(TTY_THROTTLED, &tty->flags)) {
  1774. tty_kref_put(tty);
  1775. return -1;
  1776. }
  1777. curr_write_len = tty_insert_flip_string
  1778. (tty, urb->transfer_buffer +
  1779. serial->curr_rx_urb_offset,
  1780. write_length_remaining);
  1781. serial->curr_rx_urb_offset += curr_write_len;
  1782. write_length_remaining -= curr_write_len;
  1783. tty_flip_buffer_push(tty);
  1784. }
  1785. }
  1786. if (write_length_remaining == 0) {
  1787. serial->curr_rx_urb_offset = 0;
  1788. serial->rx_urb_filled[hso_urb_to_index(serial, urb)] = 0;
  1789. }
  1790. tty_kref_put(tty);
  1791. return write_length_remaining;
  1792. }
  1793. /* Base driver functions */
  1794. static void hso_log_port(struct hso_device *hso_dev)
  1795. {
  1796. char *port_type;
  1797. char port_dev[20];
  1798. switch (hso_dev->port_spec & HSO_PORT_MASK) {
  1799. case HSO_PORT_CONTROL:
  1800. port_type = "Control";
  1801. break;
  1802. case HSO_PORT_APP:
  1803. port_type = "Application";
  1804. break;
  1805. case HSO_PORT_GPS:
  1806. port_type = "GPS";
  1807. break;
  1808. case HSO_PORT_GPS_CONTROL:
  1809. port_type = "GPS control";
  1810. break;
  1811. case HSO_PORT_APP2:
  1812. port_type = "Application2";
  1813. break;
  1814. case HSO_PORT_PCSC:
  1815. port_type = "PCSC";
  1816. break;
  1817. case HSO_PORT_DIAG:
  1818. port_type = "Diagnostic";
  1819. break;
  1820. case HSO_PORT_DIAG2:
  1821. port_type = "Diagnostic2";
  1822. break;
  1823. case HSO_PORT_MODEM:
  1824. port_type = "Modem";
  1825. break;
  1826. case HSO_PORT_NETWORK:
  1827. port_type = "Network";
  1828. break;
  1829. default:
  1830. port_type = "Unknown";
  1831. break;
  1832. }
  1833. if ((hso_dev->port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  1834. sprintf(port_dev, "%s", dev2net(hso_dev)->net->name);
  1835. } else
  1836. sprintf(port_dev, "/dev/%s%d", tty_filename,
  1837. dev2ser(hso_dev)->minor);
  1838. dev_dbg(&hso_dev->interface->dev, "HSO: Found %s port %s\n",
  1839. port_type, port_dev);
  1840. }
  1841. static int hso_start_net_device(struct hso_device *hso_dev)
  1842. {
  1843. int i, result = 0;
  1844. struct hso_net *hso_net = dev2net(hso_dev);
  1845. if (!hso_net)
  1846. return -ENODEV;
  1847. /* send URBs for all read buffers */
  1848. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1849. /* Prep a receive URB */
  1850. usb_fill_bulk_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1851. hso_dev->usb,
  1852. usb_rcvbulkpipe(hso_dev->usb,
  1853. hso_net->in_endp->
  1854. bEndpointAddress & 0x7F),
  1855. hso_net->mux_bulk_rx_buf_pool[i],
  1856. MUX_BULK_RX_BUF_SIZE, read_bulk_callback,
  1857. hso_net);
  1858. /* Put it out there so the device can send us stuff */
  1859. result = usb_submit_urb(hso_net->mux_bulk_rx_urb_pool[i],
  1860. GFP_NOIO);
  1861. if (result)
  1862. dev_warn(&hso_dev->usb->dev,
  1863. "%s failed mux_bulk_rx_urb[%d] %d\n", __func__,
  1864. i, result);
  1865. }
  1866. return result;
  1867. }
  1868. static int hso_stop_net_device(struct hso_device *hso_dev)
  1869. {
  1870. int i;
  1871. struct hso_net *hso_net = dev2net(hso_dev);
  1872. if (!hso_net)
  1873. return -ENODEV;
  1874. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  1875. if (hso_net->mux_bulk_rx_urb_pool[i])
  1876. usb_kill_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  1877. }
  1878. if (hso_net->mux_bulk_tx_urb)
  1879. usb_kill_urb(hso_net->mux_bulk_tx_urb);
  1880. return 0;
  1881. }
  1882. static int hso_start_serial_device(struct hso_device *hso_dev, gfp_t flags)
  1883. {
  1884. int i, result = 0;
  1885. struct hso_serial *serial = dev2ser(hso_dev);
  1886. if (!serial)
  1887. return -ENODEV;
  1888. /* If it is not the MUX port fill in and submit a bulk urb (already
  1889. * allocated in hso_serial_start) */
  1890. if (!(serial->parent->port_spec & HSO_INTF_MUX)) {
  1891. for (i = 0; i < serial->num_rx_urbs; i++) {
  1892. usb_fill_bulk_urb(serial->rx_urb[i],
  1893. serial->parent->usb,
  1894. usb_rcvbulkpipe(serial->parent->usb,
  1895. serial->in_endp->
  1896. bEndpointAddress &
  1897. 0x7F),
  1898. serial->rx_data[i],
  1899. serial->rx_data_length,
  1900. hso_std_serial_read_bulk_callback,
  1901. serial);
  1902. result = usb_submit_urb(serial->rx_urb[i], flags);
  1903. if (result) {
  1904. dev_warn(&serial->parent->usb->dev,
  1905. "Failed to submit urb - res %d\n",
  1906. result);
  1907. break;
  1908. }
  1909. }
  1910. } else {
  1911. mutex_lock(&serial->shared_int->shared_int_lock);
  1912. if (!serial->shared_int->use_count) {
  1913. result =
  1914. hso_mux_submit_intr_urb(serial->shared_int,
  1915. hso_dev->usb, flags);
  1916. }
  1917. serial->shared_int->use_count++;
  1918. mutex_unlock(&serial->shared_int->shared_int_lock);
  1919. }
  1920. if (serial->tiocmget)
  1921. tiocmget_submit_urb(serial,
  1922. serial->tiocmget,
  1923. serial->parent->usb);
  1924. return result;
  1925. }
  1926. static int hso_stop_serial_device(struct hso_device *hso_dev)
  1927. {
  1928. int i;
  1929. struct hso_serial *serial = dev2ser(hso_dev);
  1930. struct hso_tiocmget *tiocmget;
  1931. if (!serial)
  1932. return -ENODEV;
  1933. for (i = 0; i < serial->num_rx_urbs; i++) {
  1934. if (serial->rx_urb[i]) {
  1935. usb_kill_urb(serial->rx_urb[i]);
  1936. serial->rx_urb_filled[i] = 0;
  1937. }
  1938. }
  1939. serial->curr_rx_urb_idx = 0;
  1940. serial->curr_rx_urb_offset = 0;
  1941. if (serial->tx_urb)
  1942. usb_kill_urb(serial->tx_urb);
  1943. if (serial->shared_int) {
  1944. mutex_lock(&serial->shared_int->shared_int_lock);
  1945. if (serial->shared_int->use_count &&
  1946. (--serial->shared_int->use_count == 0)) {
  1947. struct urb *urb;
  1948. urb = serial->shared_int->shared_intr_urb;
  1949. if (urb)
  1950. usb_kill_urb(urb);
  1951. }
  1952. mutex_unlock(&serial->shared_int->shared_int_lock);
  1953. }
  1954. tiocmget = serial->tiocmget;
  1955. if (tiocmget) {
  1956. wake_up_interruptible(&tiocmget->waitq);
  1957. usb_kill_urb(tiocmget->urb);
  1958. }
  1959. return 0;
  1960. }
  1961. static void hso_serial_common_free(struct hso_serial *serial)
  1962. {
  1963. int i;
  1964. if (serial->parent->dev)
  1965. device_remove_file(serial->parent->dev, &dev_attr_hsotype);
  1966. tty_unregister_device(tty_drv, serial->minor);
  1967. for (i = 0; i < serial->num_rx_urbs; i++) {
  1968. /* unlink and free RX URB */
  1969. usb_free_urb(serial->rx_urb[i]);
  1970. /* free the RX buffer */
  1971. kfree(serial->rx_data[i]);
  1972. }
  1973. /* unlink and free TX URB */
  1974. usb_free_urb(serial->tx_urb);
  1975. kfree(serial->tx_data);
  1976. }
  1977. static int hso_serial_common_create(struct hso_serial *serial, int num_urbs,
  1978. int rx_size, int tx_size)
  1979. {
  1980. struct device *dev;
  1981. int minor;
  1982. int i;
  1983. minor = get_free_serial_index();
  1984. if (minor < 0)
  1985. goto exit;
  1986. /* register our minor number */
  1987. serial->parent->dev = tty_register_device(tty_drv, minor,
  1988. &serial->parent->interface->dev);
  1989. dev = serial->parent->dev;
  1990. dev->driver_data = serial->parent;
  1991. i = device_create_file(dev, &dev_attr_hsotype);
  1992. /* fill in specific data for later use */
  1993. serial->minor = minor;
  1994. serial->magic = HSO_SERIAL_MAGIC;
  1995. spin_lock_init(&serial->serial_lock);
  1996. serial->num_rx_urbs = num_urbs;
  1997. /* RX, allocate urb and initialize */
  1998. /* prepare our RX buffer */
  1999. serial->rx_data_length = rx_size;
  2000. for (i = 0; i < serial->num_rx_urbs; i++) {
  2001. serial->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  2002. if (!serial->rx_urb[i]) {
  2003. dev_err(dev, "Could not allocate urb?\n");
  2004. goto exit;
  2005. }
  2006. serial->rx_urb[i]->transfer_buffer = NULL;
  2007. serial->rx_urb[i]->transfer_buffer_length = 0;
  2008. serial->rx_data[i] = kzalloc(serial->rx_data_length,
  2009. GFP_KERNEL);
  2010. if (!serial->rx_data[i]) {
  2011. dev_err(dev, "%s - Out of memory\n", __func__);
  2012. goto exit;
  2013. }
  2014. }
  2015. /* TX, allocate urb and initialize */
  2016. serial->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2017. if (!serial->tx_urb) {
  2018. dev_err(dev, "Could not allocate urb?\n");
  2019. goto exit;
  2020. }
  2021. serial->tx_urb->transfer_buffer = NULL;
  2022. serial->tx_urb->transfer_buffer_length = 0;
  2023. /* prepare our TX buffer */
  2024. serial->tx_data_count = 0;
  2025. serial->tx_buffer_count = 0;
  2026. serial->tx_data_length = tx_size;
  2027. serial->tx_data = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2028. if (!serial->tx_data) {
  2029. dev_err(dev, "%s - Out of memory", __func__);
  2030. goto exit;
  2031. }
  2032. serial->tx_buffer = kzalloc(serial->tx_data_length, GFP_KERNEL);
  2033. if (!serial->tx_buffer) {
  2034. dev_err(dev, "%s - Out of memory", __func__);
  2035. goto exit;
  2036. }
  2037. return 0;
  2038. exit:
  2039. hso_serial_common_free(serial);
  2040. return -1;
  2041. }
  2042. /* Frees a general hso device */
  2043. static void hso_free_device(struct hso_device *hso_dev)
  2044. {
  2045. kfree(hso_dev);
  2046. }
  2047. /* Creates a general hso device */
  2048. static struct hso_device *hso_create_device(struct usb_interface *intf,
  2049. int port_spec)
  2050. {
  2051. struct hso_device *hso_dev;
  2052. hso_dev = kzalloc(sizeof(*hso_dev), GFP_ATOMIC);
  2053. if (!hso_dev)
  2054. return NULL;
  2055. hso_dev->port_spec = port_spec;
  2056. hso_dev->usb = interface_to_usbdev(intf);
  2057. hso_dev->interface = intf;
  2058. kref_init(&hso_dev->ref);
  2059. mutex_init(&hso_dev->mutex);
  2060. INIT_WORK(&hso_dev->async_get_intf, async_get_intf);
  2061. INIT_WORK(&hso_dev->async_put_intf, async_put_intf);
  2062. return hso_dev;
  2063. }
  2064. /* Removes a network device in the network device table */
  2065. static int remove_net_device(struct hso_device *hso_dev)
  2066. {
  2067. int i;
  2068. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2069. if (network_table[i] == hso_dev) {
  2070. network_table[i] = NULL;
  2071. break;
  2072. }
  2073. }
  2074. if (i == HSO_MAX_NET_DEVICES)
  2075. return -1;
  2076. return 0;
  2077. }
  2078. /* Frees our network device */
  2079. static void hso_free_net_device(struct hso_device *hso_dev)
  2080. {
  2081. int i;
  2082. struct hso_net *hso_net = dev2net(hso_dev);
  2083. if (!hso_net)
  2084. return;
  2085. /* start freeing */
  2086. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2087. usb_free_urb(hso_net->mux_bulk_rx_urb_pool[i]);
  2088. kfree(hso_net->mux_bulk_rx_buf_pool[i]);
  2089. }
  2090. usb_free_urb(hso_net->mux_bulk_tx_urb);
  2091. kfree(hso_net->mux_bulk_tx_buf);
  2092. remove_net_device(hso_net->parent);
  2093. if (hso_net->net) {
  2094. unregister_netdev(hso_net->net);
  2095. free_netdev(hso_net->net);
  2096. }
  2097. hso_free_device(hso_dev);
  2098. }
  2099. /* initialize the network interface */
  2100. static void hso_net_init(struct net_device *net)
  2101. {
  2102. struct hso_net *hso_net = netdev_priv(net);
  2103. D1("sizeof hso_net is %d", (int)sizeof(*hso_net));
  2104. /* fill in the other fields */
  2105. net->open = hso_net_open;
  2106. net->stop = hso_net_close;
  2107. net->hard_start_xmit = hso_net_start_xmit;
  2108. net->tx_timeout = hso_net_tx_timeout;
  2109. net->watchdog_timeo = HSO_NET_TX_TIMEOUT;
  2110. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  2111. net->type = ARPHRD_NONE;
  2112. net->mtu = DEFAULT_MTU - 14;
  2113. net->tx_queue_len = 10;
  2114. SET_ETHTOOL_OPS(net, &ops);
  2115. /* and initialize the semaphore */
  2116. spin_lock_init(&hso_net->net_lock);
  2117. }
  2118. /* Adds a network device in the network device table */
  2119. static int add_net_device(struct hso_device *hso_dev)
  2120. {
  2121. int i;
  2122. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2123. if (network_table[i] == NULL) {
  2124. network_table[i] = hso_dev;
  2125. break;
  2126. }
  2127. }
  2128. if (i == HSO_MAX_NET_DEVICES)
  2129. return -1;
  2130. return 0;
  2131. }
  2132. static int hso_radio_toggle(void *data, enum rfkill_state state)
  2133. {
  2134. struct hso_device *hso_dev = data;
  2135. int enabled = (state == RFKILL_STATE_ON);
  2136. int rv;
  2137. mutex_lock(&hso_dev->mutex);
  2138. if (hso_dev->usb_gone)
  2139. rv = 0;
  2140. else
  2141. rv = usb_control_msg(hso_dev->usb, usb_rcvctrlpipe(hso_dev->usb, 0),
  2142. enabled ? 0x82 : 0x81, 0x40, 0, 0, NULL, 0,
  2143. USB_CTRL_SET_TIMEOUT);
  2144. mutex_unlock(&hso_dev->mutex);
  2145. return rv;
  2146. }
  2147. /* Creates and sets up everything for rfkill */
  2148. static void hso_create_rfkill(struct hso_device *hso_dev,
  2149. struct usb_interface *interface)
  2150. {
  2151. struct hso_net *hso_net = dev2net(hso_dev);
  2152. struct device *dev = &hso_net->net->dev;
  2153. char *rfkn;
  2154. hso_net->rfkill = rfkill_allocate(&interface_to_usbdev(interface)->dev,
  2155. RFKILL_TYPE_WWAN);
  2156. if (!hso_net->rfkill) {
  2157. dev_err(dev, "%s - Out of memory\n", __func__);
  2158. return;
  2159. }
  2160. rfkn = kzalloc(20, GFP_KERNEL);
  2161. if (!rfkn) {
  2162. rfkill_free(hso_net->rfkill);
  2163. hso_net->rfkill = NULL;
  2164. dev_err(dev, "%s - Out of memory\n", __func__);
  2165. return;
  2166. }
  2167. snprintf(rfkn, 20, "hso-%d",
  2168. interface->altsetting->desc.bInterfaceNumber);
  2169. hso_net->rfkill->name = rfkn;
  2170. hso_net->rfkill->state = RFKILL_STATE_ON;
  2171. hso_net->rfkill->data = hso_dev;
  2172. hso_net->rfkill->toggle_radio = hso_radio_toggle;
  2173. if (rfkill_register(hso_net->rfkill) < 0) {
  2174. kfree(rfkn);
  2175. hso_net->rfkill->name = NULL;
  2176. rfkill_free(hso_net->rfkill);
  2177. hso_net->rfkill = NULL;
  2178. dev_err(dev, "%s - Failed to register rfkill\n", __func__);
  2179. return;
  2180. }
  2181. }
  2182. /* Creates our network device */
  2183. static struct hso_device *hso_create_net_device(struct usb_interface *interface)
  2184. {
  2185. int result, i;
  2186. struct net_device *net;
  2187. struct hso_net *hso_net;
  2188. struct hso_device *hso_dev;
  2189. hso_dev = hso_create_device(interface, HSO_INTF_MUX | HSO_PORT_NETWORK);
  2190. if (!hso_dev)
  2191. return NULL;
  2192. /* allocate our network device, then we can put in our private data */
  2193. /* call hso_net_init to do the basic initialization */
  2194. net = alloc_netdev(sizeof(struct hso_net), "hso%d", hso_net_init);
  2195. if (!net) {
  2196. dev_err(&interface->dev, "Unable to create ethernet device\n");
  2197. goto exit;
  2198. }
  2199. hso_net = netdev_priv(net);
  2200. hso_dev->port_data.dev_net = hso_net;
  2201. hso_net->net = net;
  2202. hso_net->parent = hso_dev;
  2203. hso_net->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2204. USB_DIR_IN);
  2205. if (!hso_net->in_endp) {
  2206. dev_err(&interface->dev, "Can't find BULK IN endpoint\n");
  2207. goto exit;
  2208. }
  2209. hso_net->out_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2210. USB_DIR_OUT);
  2211. if (!hso_net->out_endp) {
  2212. dev_err(&interface->dev, "Can't find BULK OUT endpoint\n");
  2213. goto exit;
  2214. }
  2215. SET_NETDEV_DEV(net, &interface->dev);
  2216. /* registering our net device */
  2217. result = register_netdev(net);
  2218. if (result) {
  2219. dev_err(&interface->dev, "Failed to register device\n");
  2220. goto exit;
  2221. }
  2222. /* start allocating */
  2223. for (i = 0; i < MUX_BULK_RX_BUF_COUNT; i++) {
  2224. hso_net->mux_bulk_rx_urb_pool[i] = usb_alloc_urb(0, GFP_KERNEL);
  2225. if (!hso_net->mux_bulk_rx_urb_pool[i]) {
  2226. dev_err(&interface->dev, "Could not allocate rx urb\n");
  2227. goto exit;
  2228. }
  2229. hso_net->mux_bulk_rx_buf_pool[i] = kzalloc(MUX_BULK_RX_BUF_SIZE,
  2230. GFP_KERNEL);
  2231. if (!hso_net->mux_bulk_rx_buf_pool[i]) {
  2232. dev_err(&interface->dev, "Could not allocate rx buf\n");
  2233. goto exit;
  2234. }
  2235. }
  2236. hso_net->mux_bulk_tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  2237. if (!hso_net->mux_bulk_tx_urb) {
  2238. dev_err(&interface->dev, "Could not allocate tx urb\n");
  2239. goto exit;
  2240. }
  2241. hso_net->mux_bulk_tx_buf = kzalloc(MUX_BULK_TX_BUF_SIZE, GFP_KERNEL);
  2242. if (!hso_net->mux_bulk_tx_buf) {
  2243. dev_err(&interface->dev, "Could not allocate tx buf\n");
  2244. goto exit;
  2245. }
  2246. add_net_device(hso_dev);
  2247. hso_log_port(hso_dev);
  2248. hso_create_rfkill(hso_dev, interface);
  2249. return hso_dev;
  2250. exit:
  2251. hso_free_net_device(hso_dev);
  2252. return NULL;
  2253. }
  2254. static void hso_free_tiomget(struct hso_serial *serial)
  2255. {
  2256. struct hso_tiocmget *tiocmget = serial->tiocmget;
  2257. if (tiocmget) {
  2258. kfree(tiocmget);
  2259. if (tiocmget->urb) {
  2260. usb_free_urb(tiocmget->urb);
  2261. tiocmget->urb = NULL;
  2262. }
  2263. serial->tiocmget = NULL;
  2264. }
  2265. }
  2266. /* Frees an AT channel ( goes for both mux and non-mux ) */
  2267. static void hso_free_serial_device(struct hso_device *hso_dev)
  2268. {
  2269. struct hso_serial *serial = dev2ser(hso_dev);
  2270. if (!serial)
  2271. return;
  2272. set_serial_by_index(serial->minor, NULL);
  2273. hso_serial_common_free(serial);
  2274. if (serial->shared_int) {
  2275. mutex_lock(&serial->shared_int->shared_int_lock);
  2276. if (--serial->shared_int->ref_count == 0)
  2277. hso_free_shared_int(serial->shared_int);
  2278. else
  2279. mutex_unlock(&serial->shared_int->shared_int_lock);
  2280. }
  2281. hso_free_tiomget(serial);
  2282. kfree(serial);
  2283. hso_free_device(hso_dev);
  2284. }
  2285. /* Creates a bulk AT channel */
  2286. static struct hso_device *hso_create_bulk_serial_device(
  2287. struct usb_interface *interface, int port)
  2288. {
  2289. struct hso_device *hso_dev;
  2290. struct hso_serial *serial;
  2291. int num_urbs;
  2292. struct hso_tiocmget *tiocmget;
  2293. hso_dev = hso_create_device(interface, port);
  2294. if (!hso_dev)
  2295. return NULL;
  2296. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2297. if (!serial)
  2298. goto exit;
  2299. serial->parent = hso_dev;
  2300. hso_dev->port_data.dev_serial = serial;
  2301. if ((port & HSO_PORT_MASK) == HSO_PORT_MODEM) {
  2302. num_urbs = 2;
  2303. serial->tiocmget = kzalloc(sizeof(struct hso_tiocmget),
  2304. GFP_KERNEL);
  2305. /* it isn't going to break our heart if serial->tiocmget
  2306. * allocation fails don't bother checking this.
  2307. */
  2308. if (serial->tiocmget) {
  2309. tiocmget = serial->tiocmget;
  2310. tiocmget->urb = usb_alloc_urb(0, GFP_KERNEL);
  2311. if (tiocmget->urb) {
  2312. mutex_init(&tiocmget->mutex);
  2313. init_waitqueue_head(&tiocmget->waitq);
  2314. tiocmget->endp = hso_get_ep(
  2315. interface,
  2316. USB_ENDPOINT_XFER_INT,
  2317. USB_DIR_IN);
  2318. } else
  2319. hso_free_tiomget(serial);
  2320. }
  2321. }
  2322. else
  2323. num_urbs = 1;
  2324. if (hso_serial_common_create(serial, num_urbs, BULK_URB_RX_SIZE,
  2325. BULK_URB_TX_SIZE))
  2326. goto exit;
  2327. serial->in_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_BULK,
  2328. USB_DIR_IN);
  2329. if (!serial->in_endp) {
  2330. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2331. goto exit2;
  2332. }
  2333. if (!
  2334. (serial->out_endp =
  2335. hso_get_ep(interface, USB_ENDPOINT_XFER_BULK, USB_DIR_OUT))) {
  2336. dev_err(&interface->dev, "Failed to find BULK IN ep\n");
  2337. goto exit2;
  2338. }
  2339. serial->write_data = hso_std_serial_write_data;
  2340. /* and record this serial */
  2341. set_serial_by_index(serial->minor, serial);
  2342. /* setup the proc dirs and files if needed */
  2343. hso_log_port(hso_dev);
  2344. /* done, return it */
  2345. return hso_dev;
  2346. exit2:
  2347. hso_serial_common_free(serial);
  2348. exit:
  2349. hso_free_tiomget(serial);
  2350. kfree(serial);
  2351. hso_free_device(hso_dev);
  2352. return NULL;
  2353. }
  2354. /* Creates a multiplexed AT channel */
  2355. static
  2356. struct hso_device *hso_create_mux_serial_device(struct usb_interface *interface,
  2357. int port,
  2358. struct hso_shared_int *mux)
  2359. {
  2360. struct hso_device *hso_dev;
  2361. struct hso_serial *serial;
  2362. int port_spec;
  2363. port_spec = HSO_INTF_MUX;
  2364. port_spec &= ~HSO_PORT_MASK;
  2365. port_spec |= hso_mux_to_port(port);
  2366. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NO_PORT)
  2367. return NULL;
  2368. hso_dev = hso_create_device(interface, port_spec);
  2369. if (!hso_dev)
  2370. return NULL;
  2371. serial = kzalloc(sizeof(*serial), GFP_KERNEL);
  2372. if (!serial)
  2373. goto exit;
  2374. hso_dev->port_data.dev_serial = serial;
  2375. serial->parent = hso_dev;
  2376. if (hso_serial_common_create
  2377. (serial, 1, CTRL_URB_RX_SIZE, CTRL_URB_TX_SIZE))
  2378. goto exit;
  2379. serial->tx_data_length--;
  2380. serial->write_data = hso_mux_serial_write_data;
  2381. serial->shared_int = mux;
  2382. mutex_lock(&serial->shared_int->shared_int_lock);
  2383. serial->shared_int->ref_count++;
  2384. mutex_unlock(&serial->shared_int->shared_int_lock);
  2385. /* and record this serial */
  2386. set_serial_by_index(serial->minor, serial);
  2387. /* setup the proc dirs and files if needed */
  2388. hso_log_port(hso_dev);
  2389. /* done, return it */
  2390. return hso_dev;
  2391. exit:
  2392. if (serial) {
  2393. tty_unregister_device(tty_drv, serial->minor);
  2394. kfree(serial);
  2395. }
  2396. if (hso_dev)
  2397. hso_free_device(hso_dev);
  2398. return NULL;
  2399. }
  2400. static void hso_free_shared_int(struct hso_shared_int *mux)
  2401. {
  2402. usb_free_urb(mux->shared_intr_urb);
  2403. kfree(mux->shared_intr_buf);
  2404. mutex_unlock(&mux->shared_int_lock);
  2405. kfree(mux);
  2406. }
  2407. static
  2408. struct hso_shared_int *hso_create_shared_int(struct usb_interface *interface)
  2409. {
  2410. struct hso_shared_int *mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  2411. if (!mux)
  2412. return NULL;
  2413. mux->intr_endp = hso_get_ep(interface, USB_ENDPOINT_XFER_INT,
  2414. USB_DIR_IN);
  2415. if (!mux->intr_endp) {
  2416. dev_err(&interface->dev, "Can't find INT IN endpoint\n");
  2417. goto exit;
  2418. }
  2419. mux->shared_intr_urb = usb_alloc_urb(0, GFP_KERNEL);
  2420. if (!mux->shared_intr_urb) {
  2421. dev_err(&interface->dev, "Could not allocate intr urb?");
  2422. goto exit;
  2423. }
  2424. mux->shared_intr_buf = kzalloc(mux->intr_endp->wMaxPacketSize,
  2425. GFP_KERNEL);
  2426. if (!mux->shared_intr_buf) {
  2427. dev_err(&interface->dev, "Could not allocate intr buf?");
  2428. goto exit;
  2429. }
  2430. mutex_init(&mux->shared_int_lock);
  2431. return mux;
  2432. exit:
  2433. kfree(mux->shared_intr_buf);
  2434. usb_free_urb(mux->shared_intr_urb);
  2435. kfree(mux);
  2436. return NULL;
  2437. }
  2438. /* Gets the port spec for a certain interface */
  2439. static int hso_get_config_data(struct usb_interface *interface)
  2440. {
  2441. struct usb_device *usbdev = interface_to_usbdev(interface);
  2442. u8 config_data[17];
  2443. u32 if_num = interface->altsetting->desc.bInterfaceNumber;
  2444. s32 result;
  2445. if (usb_control_msg(usbdev, usb_rcvctrlpipe(usbdev, 0),
  2446. 0x86, 0xC0, 0, 0, config_data, 17,
  2447. USB_CTRL_SET_TIMEOUT) != 0x11) {
  2448. return -EIO;
  2449. }
  2450. switch (config_data[if_num]) {
  2451. case 0x0:
  2452. result = 0;
  2453. break;
  2454. case 0x1:
  2455. result = HSO_PORT_DIAG;
  2456. break;
  2457. case 0x2:
  2458. result = HSO_PORT_GPS;
  2459. break;
  2460. case 0x3:
  2461. result = HSO_PORT_GPS_CONTROL;
  2462. break;
  2463. case 0x4:
  2464. result = HSO_PORT_APP;
  2465. break;
  2466. case 0x5:
  2467. result = HSO_PORT_APP2;
  2468. break;
  2469. case 0x6:
  2470. result = HSO_PORT_CONTROL;
  2471. break;
  2472. case 0x7:
  2473. result = HSO_PORT_NETWORK;
  2474. break;
  2475. case 0x8:
  2476. result = HSO_PORT_MODEM;
  2477. break;
  2478. case 0x9:
  2479. result = HSO_PORT_MSD;
  2480. break;
  2481. case 0xa:
  2482. result = HSO_PORT_PCSC;
  2483. break;
  2484. case 0xb:
  2485. result = HSO_PORT_VOICE;
  2486. break;
  2487. default:
  2488. result = 0;
  2489. }
  2490. if (result)
  2491. result |= HSO_INTF_BULK;
  2492. if (config_data[16] & 0x1)
  2493. result |= HSO_INFO_CRC_BUG;
  2494. return result;
  2495. }
  2496. /* called once for each interface upon device insertion */
  2497. static int hso_probe(struct usb_interface *interface,
  2498. const struct usb_device_id *id)
  2499. {
  2500. int mux, i, if_num, port_spec;
  2501. unsigned char port_mask;
  2502. struct hso_device *hso_dev = NULL;
  2503. struct hso_shared_int *shared_int;
  2504. struct hso_device *tmp_dev = NULL;
  2505. if_num = interface->altsetting->desc.bInterfaceNumber;
  2506. /* Get the interface/port specification from either driver_info or from
  2507. * the device itself */
  2508. if (id->driver_info)
  2509. port_spec = ((u32 *)(id->driver_info))[if_num];
  2510. else
  2511. port_spec = hso_get_config_data(interface);
  2512. if (interface->cur_altsetting->desc.bInterfaceClass != 0xFF) {
  2513. dev_err(&interface->dev, "Not our interface\n");
  2514. return -ENODEV;
  2515. }
  2516. /* Check if we need to switch to alt interfaces prior to port
  2517. * configuration */
  2518. if (interface->num_altsetting > 1)
  2519. usb_set_interface(interface_to_usbdev(interface), if_num, 1);
  2520. interface->needs_remote_wakeup = 1;
  2521. /* Allocate new hso device(s) */
  2522. switch (port_spec & HSO_INTF_MASK) {
  2523. case HSO_INTF_MUX:
  2524. if ((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK) {
  2525. /* Create the network device */
  2526. if (!disable_net) {
  2527. hso_dev = hso_create_net_device(interface);
  2528. if (!hso_dev)
  2529. goto exit;
  2530. tmp_dev = hso_dev;
  2531. }
  2532. }
  2533. if (hso_get_mux_ports(interface, &port_mask))
  2534. /* TODO: de-allocate everything */
  2535. goto exit;
  2536. shared_int = hso_create_shared_int(interface);
  2537. if (!shared_int)
  2538. goto exit;
  2539. for (i = 1, mux = 0; i < 0x100; i = i << 1, mux++) {
  2540. if (port_mask & i) {
  2541. hso_dev = hso_create_mux_serial_device(
  2542. interface, i, shared_int);
  2543. if (!hso_dev)
  2544. goto exit;
  2545. }
  2546. }
  2547. if (tmp_dev)
  2548. hso_dev = tmp_dev;
  2549. break;
  2550. case HSO_INTF_BULK:
  2551. /* It's a regular bulk interface */
  2552. if (((port_spec & HSO_PORT_MASK) == HSO_PORT_NETWORK)
  2553. && !disable_net)
  2554. hso_dev = hso_create_net_device(interface);
  2555. else
  2556. hso_dev =
  2557. hso_create_bulk_serial_device(interface, port_spec);
  2558. if (!hso_dev)
  2559. goto exit;
  2560. break;
  2561. default:
  2562. goto exit;
  2563. }
  2564. usb_driver_claim_interface(&hso_driver, interface, hso_dev);
  2565. /* save our data pointer in this device */
  2566. usb_set_intfdata(interface, hso_dev);
  2567. /* done */
  2568. return 0;
  2569. exit:
  2570. hso_free_interface(interface);
  2571. return -ENODEV;
  2572. }
  2573. /* device removed, cleaning up */
  2574. static void hso_disconnect(struct usb_interface *interface)
  2575. {
  2576. hso_free_interface(interface);
  2577. /* remove reference of our private data */
  2578. usb_set_intfdata(interface, NULL);
  2579. usb_driver_release_interface(&hso_driver, interface);
  2580. }
  2581. static void async_get_intf(struct work_struct *data)
  2582. {
  2583. struct hso_device *hso_dev =
  2584. container_of(data, struct hso_device, async_get_intf);
  2585. usb_autopm_get_interface(hso_dev->interface);
  2586. }
  2587. static void async_put_intf(struct work_struct *data)
  2588. {
  2589. struct hso_device *hso_dev =
  2590. container_of(data, struct hso_device, async_put_intf);
  2591. usb_autopm_put_interface(hso_dev->interface);
  2592. }
  2593. static int hso_get_activity(struct hso_device *hso_dev)
  2594. {
  2595. if (hso_dev->usb->state == USB_STATE_SUSPENDED) {
  2596. if (!hso_dev->is_active) {
  2597. hso_dev->is_active = 1;
  2598. schedule_work(&hso_dev->async_get_intf);
  2599. }
  2600. }
  2601. if (hso_dev->usb->state != USB_STATE_CONFIGURED)
  2602. return -EAGAIN;
  2603. usb_mark_last_busy(hso_dev->usb);
  2604. return 0;
  2605. }
  2606. static int hso_put_activity(struct hso_device *hso_dev)
  2607. {
  2608. if (hso_dev->usb->state != USB_STATE_SUSPENDED) {
  2609. if (hso_dev->is_active) {
  2610. hso_dev->is_active = 0;
  2611. schedule_work(&hso_dev->async_put_intf);
  2612. return -EAGAIN;
  2613. }
  2614. }
  2615. hso_dev->is_active = 0;
  2616. return 0;
  2617. }
  2618. /* called by kernel when we need to suspend device */
  2619. static int hso_suspend(struct usb_interface *iface, pm_message_t message)
  2620. {
  2621. int i, result;
  2622. /* Stop all serial ports */
  2623. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2624. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2625. result = hso_stop_serial_device(serial_table[i]);
  2626. if (result)
  2627. goto out;
  2628. }
  2629. }
  2630. /* Stop all network ports */
  2631. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2632. if (network_table[i] &&
  2633. (network_table[i]->interface == iface)) {
  2634. result = hso_stop_net_device(network_table[i]);
  2635. if (result)
  2636. goto out;
  2637. }
  2638. }
  2639. out:
  2640. return 0;
  2641. }
  2642. /* called by kernel when we need to resume device */
  2643. static int hso_resume(struct usb_interface *iface)
  2644. {
  2645. int i, result = 0;
  2646. struct hso_net *hso_net;
  2647. /* Start all serial ports */
  2648. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2649. if (serial_table[i] && (serial_table[i]->interface == iface)) {
  2650. if (dev2ser(serial_table[i])->open_count) {
  2651. result =
  2652. hso_start_serial_device(serial_table[i], GFP_NOIO);
  2653. hso_kick_transmit(dev2ser(serial_table[i]));
  2654. if (result)
  2655. goto out;
  2656. }
  2657. }
  2658. }
  2659. /* Start all network ports */
  2660. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2661. if (network_table[i] &&
  2662. (network_table[i]->interface == iface)) {
  2663. hso_net = dev2net(network_table[i]);
  2664. if (hso_net->flags & IFF_UP) {
  2665. /* First transmit any lingering data,
  2666. then restart the device. */
  2667. if (hso_net->skb_tx_buf) {
  2668. dev_dbg(&iface->dev,
  2669. "Transmitting"
  2670. " lingering data\n");
  2671. hso_net_start_xmit(hso_net->skb_tx_buf,
  2672. hso_net->net);
  2673. hso_net->skb_tx_buf = NULL;
  2674. }
  2675. result = hso_start_net_device(network_table[i]);
  2676. if (result)
  2677. goto out;
  2678. }
  2679. }
  2680. }
  2681. out:
  2682. return result;
  2683. }
  2684. static void hso_serial_ref_free(struct kref *ref)
  2685. {
  2686. struct hso_device *hso_dev = container_of(ref, struct hso_device, ref);
  2687. hso_free_serial_device(hso_dev);
  2688. }
  2689. static void hso_free_interface(struct usb_interface *interface)
  2690. {
  2691. struct hso_serial *hso_dev;
  2692. struct tty_struct *tty;
  2693. int i;
  2694. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++) {
  2695. if (serial_table[i]
  2696. && (serial_table[i]->interface == interface)) {
  2697. hso_dev = dev2ser(serial_table[i]);
  2698. spin_lock_irq(&hso_dev->serial_lock);
  2699. tty = tty_kref_get(hso_dev->tty);
  2700. spin_unlock_irq(&hso_dev->serial_lock);
  2701. if (tty)
  2702. tty_hangup(tty);
  2703. mutex_lock(&hso_dev->parent->mutex);
  2704. tty_kref_put(tty);
  2705. hso_dev->parent->usb_gone = 1;
  2706. mutex_unlock(&hso_dev->parent->mutex);
  2707. kref_put(&serial_table[i]->ref, hso_serial_ref_free);
  2708. }
  2709. }
  2710. for (i = 0; i < HSO_MAX_NET_DEVICES; i++) {
  2711. if (network_table[i]
  2712. && (network_table[i]->interface == interface)) {
  2713. struct rfkill *rfk = dev2net(network_table[i])->rfkill;
  2714. /* hso_stop_net_device doesn't stop the net queue since
  2715. * traffic needs to start it again when suspended */
  2716. netif_stop_queue(dev2net(network_table[i])->net);
  2717. hso_stop_net_device(network_table[i]);
  2718. cancel_work_sync(&network_table[i]->async_put_intf);
  2719. cancel_work_sync(&network_table[i]->async_get_intf);
  2720. if (rfk)
  2721. rfkill_unregister(rfk);
  2722. hso_free_net_device(network_table[i]);
  2723. }
  2724. }
  2725. }
  2726. /* Helper functions */
  2727. /* Get the endpoint ! */
  2728. static struct usb_endpoint_descriptor *hso_get_ep(struct usb_interface *intf,
  2729. int type, int dir)
  2730. {
  2731. int i;
  2732. struct usb_host_interface *iface = intf->cur_altsetting;
  2733. struct usb_endpoint_descriptor *endp;
  2734. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2735. endp = &iface->endpoint[i].desc;
  2736. if (((endp->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir) &&
  2737. (usb_endpoint_type(endp) == type))
  2738. return endp;
  2739. }
  2740. return NULL;
  2741. }
  2742. /* Get the byte that describes which ports are enabled */
  2743. static int hso_get_mux_ports(struct usb_interface *intf, unsigned char *ports)
  2744. {
  2745. int i;
  2746. struct usb_host_interface *iface = intf->cur_altsetting;
  2747. if (iface->extralen == 3) {
  2748. *ports = iface->extra[2];
  2749. return 0;
  2750. }
  2751. for (i = 0; i < iface->desc.bNumEndpoints; i++) {
  2752. if (iface->endpoint[i].extralen == 3) {
  2753. *ports = iface->endpoint[i].extra[2];
  2754. return 0;
  2755. }
  2756. }
  2757. return -1;
  2758. }
  2759. /* interrupt urb needs to be submitted, used for serial read of muxed port */
  2760. static int hso_mux_submit_intr_urb(struct hso_shared_int *shared_int,
  2761. struct usb_device *usb, gfp_t gfp)
  2762. {
  2763. int result;
  2764. usb_fill_int_urb(shared_int->shared_intr_urb, usb,
  2765. usb_rcvintpipe(usb,
  2766. shared_int->intr_endp->bEndpointAddress & 0x7F),
  2767. shared_int->shared_intr_buf,
  2768. shared_int->intr_endp->wMaxPacketSize,
  2769. intr_callback, shared_int,
  2770. shared_int->intr_endp->bInterval);
  2771. result = usb_submit_urb(shared_int->shared_intr_urb, gfp);
  2772. if (result)
  2773. dev_warn(&usb->dev, "%s failed mux_intr_urb %d", __func__,
  2774. result);
  2775. return result;
  2776. }
  2777. /* operations setup of the serial interface */
  2778. static const struct tty_operations hso_serial_ops = {
  2779. .open = hso_serial_open,
  2780. .close = hso_serial_close,
  2781. .write = hso_serial_write,
  2782. .write_room = hso_serial_write_room,
  2783. .ioctl = hso_serial_ioctl,
  2784. .set_termios = hso_serial_set_termios,
  2785. .chars_in_buffer = hso_serial_chars_in_buffer,
  2786. .tiocmget = hso_serial_tiocmget,
  2787. .tiocmset = hso_serial_tiocmset,
  2788. .unthrottle = hso_unthrottle
  2789. };
  2790. static struct usb_driver hso_driver = {
  2791. .name = driver_name,
  2792. .probe = hso_probe,
  2793. .disconnect = hso_disconnect,
  2794. .id_table = hso_ids,
  2795. .suspend = hso_suspend,
  2796. .resume = hso_resume,
  2797. .reset_resume = hso_resume,
  2798. .supports_autosuspend = 1,
  2799. };
  2800. static int __init hso_init(void)
  2801. {
  2802. int i;
  2803. int result;
  2804. /* put it in the log */
  2805. printk(KERN_INFO "hso: %s\n", version);
  2806. /* Initialise the serial table semaphore and table */
  2807. spin_lock_init(&serial_table_lock);
  2808. for (i = 0; i < HSO_SERIAL_TTY_MINORS; i++)
  2809. serial_table[i] = NULL;
  2810. /* allocate our driver using the proper amount of supported minors */
  2811. tty_drv = alloc_tty_driver(HSO_SERIAL_TTY_MINORS);
  2812. if (!tty_drv)
  2813. return -ENOMEM;
  2814. /* fill in all needed values */
  2815. tty_drv->magic = TTY_DRIVER_MAGIC;
  2816. tty_drv->owner = THIS_MODULE;
  2817. tty_drv->driver_name = driver_name;
  2818. tty_drv->name = tty_filename;
  2819. /* if major number is provided as parameter, use that one */
  2820. if (tty_major)
  2821. tty_drv->major = tty_major;
  2822. tty_drv->minor_start = 0;
  2823. tty_drv->num = HSO_SERIAL_TTY_MINORS;
  2824. tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
  2825. tty_drv->subtype = SERIAL_TYPE_NORMAL;
  2826. tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  2827. tty_drv->init_termios = tty_std_termios;
  2828. hso_init_termios(&tty_drv->init_termios);
  2829. tty_set_operations(tty_drv, &hso_serial_ops);
  2830. /* register the tty driver */
  2831. result = tty_register_driver(tty_drv);
  2832. if (result) {
  2833. printk(KERN_ERR "%s - tty_register_driver failed(%d)\n",
  2834. __func__, result);
  2835. return result;
  2836. }
  2837. /* register this module as an usb driver */
  2838. result = usb_register(&hso_driver);
  2839. if (result) {
  2840. printk(KERN_ERR "Could not register hso driver? error: %d\n",
  2841. result);
  2842. /* cleanup serial interface */
  2843. tty_unregister_driver(tty_drv);
  2844. return result;
  2845. }
  2846. /* done */
  2847. return 0;
  2848. }
  2849. static void __exit hso_exit(void)
  2850. {
  2851. printk(KERN_INFO "hso: unloaded\n");
  2852. tty_unregister_driver(tty_drv);
  2853. /* deregister the usb driver */
  2854. usb_deregister(&hso_driver);
  2855. }
  2856. /* Module definitions */
  2857. module_init(hso_init);
  2858. module_exit(hso_exit);
  2859. MODULE_AUTHOR(MOD_AUTHOR);
  2860. MODULE_DESCRIPTION(MOD_DESCRIPTION);
  2861. MODULE_LICENSE(MOD_LICENSE);
  2862. MODULE_INFO(Version, DRIVER_VERSION);
  2863. /* change the debug level (eg: insmod hso.ko debug=0x04) */
  2864. MODULE_PARM_DESC(debug, "Level of debug [0x01 | 0x02 | 0x04 | 0x08 | 0x10]");
  2865. module_param(debug, int, S_IRUGO | S_IWUSR);
  2866. /* set the major tty number (eg: insmod hso.ko tty_major=245) */
  2867. MODULE_PARM_DESC(tty_major, "Set the major tty number");
  2868. module_param(tty_major, int, S_IRUGO | S_IWUSR);
  2869. /* disable network interface (eg: insmod hso.ko disable_net=1) */
  2870. MODULE_PARM_DESC(disable_net, "Disable the network interface");
  2871. module_param(disable_net, int, S_IRUGO | S_IWUSR);