hso.c 83 KB

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