hso.c 85 KB

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